面向物件 子類中 < 過載 重寫 >父類的方法
阿新 • • 發佈:2019-02-20
<?php header("Content-Type: text/html; charset=utf-8"); class person{ public $name; public $age; public $sex; public function __construct($name,$age,$sex){ $this -> name = $name; $this -> age = $age; $this -> sex = $sex; } public function say(){ echo "名字:{$this -> name},年齡:{$this -> age},性別:{$this -> sex}"; } } /* * 重寫:宣告 一個與父類中同名的方法 * * 過載:parent:: 父類中的方法名 * */ //第一個 class teacher extends person{ public $tesch; public function __construct($name,$age,$sex,$tesch){ /* $this -> name = $name; $this -> age = $age; $this -> sex = $sex;*/ /*呼叫父類方法*/ parent::__construct($name,$age,$sex); $this -> tesch = $tesch; } public function say(){ /*呼叫父類方法*/ parent::say(); echo ",學科:{$this -> tesch}"; } } //第二個 class stud extends person{ public $tesch; public function __construct($name,$age,$sex,$tesch){ parent::__construct($name,$age,$sex); $this -> tesch = $tesch; } public function say(){ parent::say(); echo ",船長:{$this -> tesch}"; } } $teacher= new teacher('娜美',20,'女','航海士'); $teacher -> say(); echo '<hr/>'; $stud= new stud('路飛',19,'男','玩'); $stud -> say(); ?>