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