首頁 >後端開發 >php教程 >物件導向PHP(二)

物件導向PHP(二)

WBOY
WBOY原創
2016-07-29 09:16:21937瀏覽

<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></code>

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。

以上就介紹了物件導向PHP(二),包括了方面的內容,希望對PHP教程有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn