利用后期静态绑定技术,实现在父类调用子类中重写的静态成员

Original 2019-04-08 11:22:29 276
abstract:<?php /**  * Created by PhpStorm.  * User: hp  * Date: 2019/4/8  * Time: 10:09  */ class Boy {     p
<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2019/4/8
 * Time: 10:09
 */

class Boy

{
    public static $money = 10000;

    public static function getClass()
    {
        return __CLASS__;
    }

    public static function getMoney()
    {
        return static::getClass().'赚了'.static::$money.'元';
    }

}

class Girl extends Boy
{
    public static $money = 7000;

    public static function getClass()

    {
        return __CLASS__;
    }
}

echo Boy::getClass(),'<br>';

echo Boy::getMoney(),'<br>';

echo Girl::$money,'<br>';

echo Girl::getClass(),'<br>';

echo Girl::getMoney();


Correcting teacher:查无此人Correction time:2019-04-08 16:06:18
Teacher's summary:完成的不错。类是php中的大知识,要好好学习。

Release Notes

Popular Entries