返回 父类调用子类中...... 登陆

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

连界 现代 周伟 2019-06-09 16:32:12 207

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

最新手记推荐

• 用composer安装thinkphp框架的步骤 • 省市区接口说明 • 用thinkphp,后台新增栏目 • 管理员添加编辑删除 • 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消 回复 发送
  • PHP中文网