亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

利用后期靜態(tài)綁定技術(shù),實(shí)現(xiàn)在父類調(diào)用子類中重寫的靜態(tài)成員

原創(chuàng) 2019-03-26 15:30:03 254
摘要:<?php echo '<h3>類的繼承和代碼復(fù)用</h3>'; class Father { public $name; protected $age; private $salary; public static $sex = 'male';
<?php
echo '<h3>類的繼承和代碼復(fù)用</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>';

幾個(gè)小例子合在一起寫的

批改老師:天蓬老師批改時(shí)間:2019-03-26 16:52:15
老師總結(jié):重載主要是指方法, 方法的重載, 提供一種容錯(cuò)機(jī)制, 向用戶提供了友好的訪問(wèn)接口

發(fā)布手記

熱門詞條