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