* 1.使用關(guān)鍵字:interface
* 2.類(lèi)是對(duì)象的模板,接口是類(lèi)的模板
* 3.接口看作是一個(gè)特殊的類(lèi)
* 4.接口中的方法,只聲明不實(shí)現(xiàn),與抽象類(lèi)一樣
* 5.接口中的方法必須是html' target='_blank'>public,支持static
* 6.接口中可以聲明類(lèi)常量const,但不允許被類(lèi)或子接口覆蓋
* 7.用類(lèi)實(shí)現(xiàn)一個(gè)接口使用implements 關(guān)鍵字
* 8.一個(gè)類(lèi)可以實(shí)現(xiàn)多個(gè)接口,多個(gè)接口之間用逗號(hào)分開(kāi)
* 9.接口之間也可以使用關(guān)鍵字extends繼承
* 10.一個(gè)類(lèi)實(shí)多個(gè)接口時(shí),方法不可以重名
//聲明接口:動(dòng)物
if (!interface_exists( Animal )) { interface Animal //接口常量 const status = viable //能存活的 //接口方法:飼養(yǎng)時(shí)吃什么 public function feeding($foods);//聲明類(lèi)Cat,并實(shí)現(xiàn)接口Animalif (interface_exists( Animal )) { class Cat implements Animal private $name = 貓 //在類(lèi)中必須實(shí)現(xiàn)接口中的方法feeding() public function feeding($foods) return $this- name. 吃 .$foods;//實(shí)例化Dog類(lèi),echo (new Cat())- feeding( 老鼠 echo hr //再定義一個(gè)接口:動(dòng)物的特性if (!interface_exists( Feature )) { interface Feature //接口方法 public function hobby($hobby);//聲明一個(gè)類(lèi)Dog,實(shí)現(xiàn)了二個(gè)接口: Animal,Featureif (interface_exists( Animal ) interface_exists( Feature )) { class Dog implements Animal, Feature private $name = 狗 //必須實(shí)現(xiàn)接口Animal中的feeding()方法 public function feeding($foods)// return $this- name. 吃 .$foods; //改成鏈?zhǔn)?echo $this- name. 吃 .$foods; return $this; //必須實(shí)現(xiàn)接口Feature中的hobby()方法 public function hobby($hobby)// return $hobby; //改成鏈?zhǔn)?echo $hobby; return $this;}
//實(shí)例化Dog類(lèi)
echo (new Dog())- feeding( 肉 echo (new Dog())- hobby( 忠誠(chéng),勇敢,不離不棄~~
//大家想想如何將上面的二個(gè)方法調(diào)用改成鏈?zhǔn)?
//注意:先把上面的實(shí)例化調(diào)用語(yǔ)句注釋掉,否則下面的鏈?zhǔn)秸{(diào)用不生效
(new Dog)- feeding( 骨頭 )- hobby( ,可愛(ài),溫順,聽(tīng)話~~
以上就是php中的接口interface的詳細(xì)內(nèi)容,PHP教程
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選