abstract:<?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>';
几个小例子合在一起写的
Correcting teacher:天蓬老师Correction time:2019-03-26 16:52:15
Teacher's summary:重载主要是指方法, 方法的重载, 提供一种容错机制, 向用户提供了友好的访问接口