Rumah >pembangunan bahagian belakang >tutorial php >面向对象PHP(二)
<code><span><span><?php </span><span>/** * 1.子类与父类方法相同可以对父类方法重写 * 2.如果不希望父类被重写,用final关键字修饰,可以修饰类中方法,也可以是一个类(该类所有方法均不能被重写) */</span>header(<span>"Content-Type: text/html; charset=utf-8"</span>);<span>//去除中文乱码</span><span>final</span><span><span>class</span><span>BaseClass</span>{</span><span>final</span><span>public</span><span><span>function</span><span>test</span><span>()</span>{</span><span>echo</span><span>"BaseClass::test called.<br>"</span>; } <span>public</span><span><span>function</span><span>test1</span><span>()</span>{</span><span>echo</span><span>"BaseClass::test1 called.<br>"</span>; } } <span><span>class</span><span>ChildClass</span><span>extends</span><span>BaseClass</span> {</span><span>public</span><span><span>function</span><span>test</span><span>(<span>$value</span>=null)</span>{</span><span>//没有默认值会报错</span><span>echo</span><span>"ChildClass::test called "</span>.<span>$value</span>.<span>"<br>"</span>; } } <span>$obj</span>=<span>new</span> ChildClass(); <span>$obj</span>->test(<span>"'222'"</span>); <span>$obj</span>->test1(); <span>?></span></span></span></code>
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了面向对象PHP(二),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。