Home > php教程 > php手册 > body text

PHP5接口和PHP5抽象类的语法介绍

WBOY
Release: 2016-06-13 11:06:00
Original
838 people have browsed it

大家对具体PHP5抽象类的代码如下:

<ol class="dp-xml">
<li class="alt"><span><span>abstract class AbstractClass {     </span></span></li>
<li class=""><span>   abstract public function test();     </span></li>
<li class="alt"><span>}     </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>class ImplementedClass extends AbstractClass {     </span></li>
<li class=""><span>   public function test() {     </span></li>
<li class="alt"><span>       echo "ImplementedClass::test() called. ";     </span></li>
<li class=""><span>   }     </span></li>
<li class="alt"><span>}     </span></li>
<li class=""><span>    </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">o</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> ImplementedClass;     </span>
</li>
<li class="">
<span>$o-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>test();    </span>
</li>
</ol>
Copy after login

PHP 5也支持接口的概念,并为之引入了interface和implements关键字。和Java一样,PHP 5使用接口也实现类似于“多重继承”的效果。PHP5接口语法如下:

<ol class="dp-xml">
<li class="alt"><span><span>interface displayable {     </span></span></li>
<li class=""><span>  function display();     </span></li>
<li class="alt"><span>}     </span></li>
<li class=""><span>interface printable {     </span></li>
<li class="alt"><span>  function doprint();     </span></li>
<li class=""><span>}     </span></li>
<li class="alt"><span>    </span></li>
<li class=""><span>class foo implements displayable,printable {     </span></li>
<li class="alt"><span>  function display() {     </span></li>
<li class=""><span>    // code     </span></li>
<li class="alt"><span>  }   function doprint() {     </span></li>
<li class=""><span>    // code     </span></li>
<li class="alt"><span>  }     </span></li>
<li class=""><span>}    </span></li>
</ol>
Copy after login

抽象类和PHP5接口的引入使PHP成了一个完全面向对象的语


source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template