重载可以重写父类中指定方法所实现的功能 代码示例: <?php class father { //定义father 类 public function printNUM() { echo 10; } } class child extends father { //子类child 继承 father 类 public function printNUM() { //重写 father 类中的方法,实现重载 echo 10 * 10; } } $test1 = new father(); $test1->printNUM(); //输出10 echo "<br>"; $test2 = new child(); $test2->printNUM(); // 输出100 ?> 运行结果: 10 100
public function __construct() { parent::__construct(); }
{ $this->name = $na; $this->age = $ag; $this->person = $perso; } public function act() { echo $this->person . " name is " . $this->name . "<br/>"; } } class father extends human { public function work() { echo $this->person . " go to office.<br/>"; } } class mother extends human { public function work() { echo $this->person . " cook fish.<br/>"; echo $this->person . " age is " . $this->age; } } $father_obj = new father("Abdul", "45", "Father"); $father_obj->work(); $father_obj->act(); $mother_obj = new mother("Sufia", "38", "Mother"); $mother_obj->act(); $mother_obj->work();