首页 > 后端开发 > php教程 > PHP学习笔记5-类的继承/步骤重写

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

WBOY
发布: 2016-06-13 12:22:31
原创
852 人浏览过

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

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板