父类调用子类中重写的静态成员

Original 2019-06-09 16:32:12 215
abstract:<?phpclass Father{    public static $money = 5000;    public static function getClass()    {        return __CLASS__;    }    p

<?php
class Father
{
   public static $money = 5000;
   public static function getClass()
   {
       return __CLASS__;
   }
   public static function getMoney()
   {
       //在静态继承的上下文环境中,动态设置静态成员的调用者
       //实现在父类中调用了子类中重写的getClass()方法和$money属性
       return static::getClass() . '==>' . static::$money;
   }
}

//定义子类,继承自Father
class Son extends Father
{
   //覆写父类中的静态属性
   public static $money = 3000;
   //覆写父类中的静态方法
   public static function getClass()
   {
       return __CLASS__;
   }
}
echo Father::getClass() . '<br>';
echo Father::getMoney() . '<br>';
//调用子类Son类中的静态成员
echo Son::$money . '<br>';
echo Son::getClass() . '<br>';
echo '<hr>';
//在子类中调用父类中的方法
echo Son::getMoney() . '<br>';

Release Notes

Popular Entries