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

Original 2019-06-09 16:34:47 300
abstract:后期静态绑定能实现动态设置静态成员的调用者类中属性和方法的重载感觉好难理解 ,一般是应用在什么场景呢?<?phpclass Father{    public static $money = 5000;    public static function getClass()    {       &n
  1. 后期静态绑定能实现动态设置静态成员的调用者

  2. 类中属性和方法的重载感觉好难理解 ,一般是应用在什么场景呢?

<?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>';

Correcting teacher:查无此人Correction time:2019-06-10 10:17:37
Teacher's summary:完成的不错。学完php的类以后,就算入门了,后面要多练习数据库的增删查改。继续加油。

Release Notes

Popular Entries