static后期静态绑定

Original 2018-11-09 16:10:23 171
abstract:<?php //后期静态绑定(延迟静态绑定)class Father{ public static $money=1000000; public static function getClass() { //返回当前的类 return __CLASS__; } public static function getMoney()    {  &nbs
<?php 

//后期静态绑定(延迟静态绑定)

class Father{

public static $money=1000000;

public static function getClass()
{
//返回当前的类
return __CLASS__;
}
public static function getMoney()
    {
        // return self::getClass() . '::' . self::$money;
        //后期静态绑定,使用static,在静态继承的上下文中,动态的与调用类绑定
        return static::getClass() . '=>' . static::$money;
    }

}
//定义子类,继承自 Father
class Son extends Father{

public static $money=2000000;

public static function getClass()
    {
        
    //返回静态成员$money
       return static::$money;
    }

}

echo father::getClass().'<br>';
echo father::getMoney().'<br>';
echo son::getMoney().'<br>';//子类调用父类的方法
echo son::getClass();//子类调用自己的方法

//使用static 静态来继承上下。表达不出来啦!!!!
?>


Correcting teacher:韦小宝Correction time:2018-11-09 16:11:37
Teacher's summary:嗯!不错啊!写的很完整!课后记得总结归纳!多多练习!继续加油!

Release Notes

Popular Entries