Home  >  Article  >  php教程  >  深入探讨PHP类的继承

深入探讨PHP类的继承

WBOY
WBOYOriginal
2016-06-13 11:10:23780browse

这就像人类生儿育女一样(为什么一定要生儿育女呢,难道就是为了防老!不得而知呀),你把自己的一些基因和你老婆的基因各拿出来一些,再生成一个新的个体,这个新的个性一定会包含你们两个人身上的特征,这就是生物上关于遗传(继承)的一个解说。在编程的世界是这个遗传就是继承!

首先在了解了继承的一些活生生的原理后,我想再来看PHP类的继承是不是不再那么神秘。或许它本来就不神秘,是因为我们自己太复杂了吧。要有继承就得有一个“根源”,这“根源”你可能想像你以后生了儿子或者女儿,他们会从你这里得到一些“东西(属性和方法)”,这样你的“后代”就是持有你的(根源)所有一些特征了。下面通过语法来描述一下这个在PHP的面向对象中是怎么表达吧(不可能像人类那么直接啦,结婚,过段时间你的后代就产生了)

1.产生“根源”类(父类或者基类)

语法:class father{

}

1.产生“后代”(子类)

语法:class son extends father{

}

说明:父类只是一个平常的类,要想有后代你只需要在平常类后加一个extends关键字就OK了,这样你的子类只拥有了父类所有的属性和方法了。其实就是这么简单罢了。

下面让我们做点实际点事情吧,毕竟PHP类的继承中定义一个父类和子类是要完成某个任务的吧!在这里这个任务比较单调,就拿人来说,人有名字(属性),人要睡觉还要吃饭(方法)。我们就用这个基本的任务要完成本节的知识吧。

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php   </span></span></span></li>
<li><span>class father{   </span></li>
<li class="alt"><span>protected $name;   </span></li>
<li><span>function __construct($name){   </span></li>
<li class="alt">
<span>$this-</span><span class="tag">></span><span class="attribute">name</span><span>=$name;   </span>
</li>
<li><span>}   </span></li>
<li class="alt"><span>function __destruct(){   </span></li>
<li>
<span>echo "</span><span class="tag"><span> </span><span class="tag-name">p</span><span class="tag">></span><span>{$this-</span><span class="tag">></span><span>name}也是要死的</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">/></span><span class="tag"><span> /p</span><span class="tag">></span><span>";   </span></span></span></span>
</li>
<li class="alt"><span>}   </span></li>
<li><span>//这个就是所谓的构造函数,用来初始化   </span></li>
<li class="alt"><span>function go_to_sleeping(){   </span></li>
<li>
<span>echo "</span><span class="tag"><span> </span><span class="tag-name">p</span><span class="tag">></span><span>{$this-</span><span class="tag">></span><span>name}想睡觉.</span><span class="tag"><span> /p</span><span class="tag">></span><span>";   </span></span></span>
</li>
<li class="alt"><span>}   </span></li>
<li><span>function eat(){   </span></li>
<li class="alt">
<span>echo "</span><span class="tag"><span> </span><span class="tag-name">p</span><span class="tag">></span><span>{$this-</span><span class="tag">></span><span>name}想吃饭.</span><span class="tag"><span> /p</span><span class="tag">></span><span>";   </span></span></span>
</li>
<li><span>}   </span></li>
<li class="alt"><span>}   </span></li>
<li><span>class son extends father{   </span></li>
<li class="alt"><span>function playing(){   </span></li>
<li><span>//小孩子会很调皮的,当然他也是要吃要睡的生物   </span></li>
<li class="alt">
<span>echo "</span><span class="tag"><span> </span><span class="tag-name">p</span><span class="tag">></span><span>{$this-</span><span class="tag">></span><span>name}正在捣蛋...</span><span class="tag"><span> /p</span><span class="tag">></span><span>";   </span></span></span>
</li>
<li><span>}   </span></li>
<li class="alt"><span>}   </span></li>
<li>
<span>$</span><span class="attribute">your_father</span><span>=</span><span class="attribute-value">new</span><span> father("老爸");   </span>
</li>
<li class="alt">
<span>$your_father-</span><span class="tag">></span><span>go_to_sleeping();   </span>
</li>
<li>
<span>$your_father-</span><span class="tag">></span><span>eat();   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">my_son</span><span>=</span><span class="attribute-value">new</span><span> son('宝贝');   </span>
</li>
<li>
<span>$my_son-</span><span class="tag">></span><span>go_to_sleeping();   </span>
</li>
<li class="alt">
<span>$my_son-</span><span class="tag">></span><span>eat();   </span>
</li>
<li>
<span>$my_son-</span><span class="tag">></span><span>playing();   </span>
</li>
<li class="alt">
<span class="tag">?></span><span>   </span>
</li>
<li><span class="tag"><span> ?php  </span></span></li>
<li class="alt"><span>class father{  </span></li>
<li><span>protected $name;  </span></li>
<li class="alt"><span>function __construct($name){  </span></li>
<li>
<span>$this-</span><span class="tag">></span><span class="attribute">name</span><span>=$name;  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>function __destruct(){  </span></li>
<li class="alt">
<span>echo "</span><span class="tag"><span> </span><span class="tag-name">p</span><span class="tag">></span><span>{$this-</span><span class="tag">></span><span>name}也是要死的</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">/></span><span class="tag"><span> /p</span><span class="tag">></span><span>";  </span></span></span></span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>//这个就是所谓的构造函数,用来初始化  </span></li>
<li><span>function go_to_sleeping(){  </span></li>
<li class="alt">
<span>echo "</span><span class="tag"><span> </span><span class="tag-name">p</span><span class="tag">></span><span>{$this-</span><span class="tag">></span><span>name}想睡觉.</span><span class="tag"><span> /p</span><span class="tag">></span><span>";  </span></span></span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>function eat(){  </span></li>
<li>
<span>echo "</span><span class="tag"><span class="tag-name">p</span><span class="tag">></span><span>{$this-</span><span class="tag">></span><span>name}想吃饭.</span><span class="tag"></span><span class="tag-name">p</span><span class="tag">></span><span>";  </span></span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>class son extends father{  </span></li>
<li><span>function playing(){  </span></li>
<li class="alt"><span>//小孩子会很调皮的,当然他也是要吃要睡的生物  </span></li>
<li>
<span>echo "</span><span class="tag"><span> </span><span class="tag-name">p</span><span class="tag">></span><span>{$this-</span><span class="tag">></span><span>name}正在捣蛋...</span><span class="tag"><span> /p</span><span class="tag">></span><span>";  </span></span></span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>}  </span></li>
<li class="alt">
<span>$</span><span class="attribute">your_father</span><span>=</span><span class="attribute-value">new</span><span> father("老爸");  </span>
</li>
<li>
<span>$your_father-</span><span class="tag">></span><span>go_to_sleeping();  </span>
</li>
<li class="alt">
<span>$your_father-</span><span class="tag">></span><span>eat();  </span>
</li>
<li>
<span>$</span><span class="attribute">my_son</span><span>=</span><span class="attribute-value">new</span><span> son('宝贝');  </span>
</li>
<li class="alt">
<span>$my_son-</span><span class="tag">></span><span>go_to_sleeping();  </span>
</li>
<li>
<span>$my_son-</span><span class="tag">></span><span>eat();  </span>
</li>
<li class="alt">
<span>$my_son-</span><span class="tag">></span><span>playing();  </span>
</li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>

解析:在我们的第一个使用继承的范例里使用了PHP的构造函数中所提到的构造函数和PHP类的封装中的关键字,如果有什么不明白的就去看看吧!我可不想再多说了,中午没想睡觉呀.说说这段小程序吧。

在类的father里,我们定义了一般的特征,比如人的名字,人要吃饭和睡觉,然后在它的子类(后代)我们定义一个个性化的方法(playing),毕竟人与人之间是有不同的地方吧。我们使用构造函数来初始化这个人名,当然也使用了析构函数来“消灭”掉对象,但你可能没有发现在子类里内是没有构造函数和析构函数的,所以子类是继承了父的所有方法,要不然你怎么能$my_son->go_to_sleeping();这样调用呢,这就是PHP类的继承了。
 


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn