更改Man.php文件名为People.php,加入代码:
1 public function hi(){2 return $this->_name.' say hi';3 }
新建文件Man.php:
1 <?php 2 /** 3 * Created by PhpStorm. 4 * User: Administrator 5 * Date: 2015/6/29 6 * Time: 11:18 7 */ 8 9 require_once 'People.php';10 11 class Man extends People{//extends:继承People类12 public function __construct($age,$name){13 parent::__construct($age,$name,'男');14 }15 16 public function hi(){//重写People类的hi方法17 // echo parent::hi();//保留父类hi方法18 echo 'Man '.$this->getName().' say hi';19 }20 }
如果保留父类方法,可以这么写parent::hi();这里不保留,添加下面一行
修改index.php文件:
1 require_once 'Man.php';2 $m = new Man(10,'Vito');3 echo $m->hi();
最后在网页上可以看到成功输出的信息:Man Vito say hi