abstrait:<?php echo '<h3>類的繼承和代碼復用</h3>'; class Father { public $name; protected $age; private $salary; public static $sex = 'male';
<?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>';
幾個小例子合在一起寫的
Professeur correcteur:天蓬老師Temps de correction:2019-03-26 16:52:15
Résumé du professeur:重載主要是指方法, 方法的重載, 提供一種容錯機制, 向用戶提供了友好的訪問接口