Rumah > pembangunan bahagian belakang > tutorial php > PHP学习笔记5-类的继承/步骤重写

PHP学习笔记5-类的继承/步骤重写

WBOY
Lepaskan: 2016-06-13 12:22:31
asal
853 orang telah melayarinya

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> }
Salin selepas log masuk

新建文件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> }
Salin selepas log masuk

如果保留父类方法,可以这么写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();
Salin selepas log masuk

最后在网页上可以看到成功输出的信息:Man Vito say hi

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan