返回利用后期静态绑......登陆

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

靖哥哥2019-03-26 15:30:03245
<?php
echo '<h3>类的继承和代码复用</h3>';
class Father
{
public $name;
protected $age;
private $salary;
public static $sex = 'male';
public function __construct($name,$age,$salary)
{
$this->name = $name;
$this->age = $age;
$this->salary = $salary;
}
public function play($play)
{
return $this->name.' 在 '.$play;
}
//private method
private function save()
{
return $this->salary.'save the money!';
}
public function getAge()
{
return $this->age;
}
public static function getClass()
{
return __CLASS__;
}
public static function getInfo()
{
return static::$sex.' and '.static::getClass();
}
}
class Son extends Father
{
//subclass rewirte father method
public static $sex = 'famale';
public static function getClass()
{
return __CLASS__;
}
public function play($play)
{
echo $this->name.'<br>';
return $this->name.' is do : '.$play.parent::$sex;
}
}
$father = new Father('Fming',42,10000);
$son = new Son('sming',21,5000);
echo 'attribute extends: '.$son->name.'--'.$son->getAge().'<br>';
echo 'method extends: '.$son->play('is running!').'<br>';
echo $father->play('working').'<br>';
echo Father::getInfo().'<br>';
echo Son::getInfo().'<br>';

几个小例子合在一起写的

最新手记推荐

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

全部回复(0)我要回复

暂无评论~
  • 取消回复发送