国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

淺析php接口操作interface關鍵字的用法及應用實例

2024-09-04 11:43:48
字體:
來源:轉載
供稿:網友

interface是面向對象編程語言中接口操作的關鍵字,功能是把所需成員組合起來,以封裝一定功能的集合,本文我們來講講interface基本知識及用法實例.

接口是一種約束形式,其中只包括成員定義,不包含成員實現的內容,用接口(interface),你可以指定某個類必須實現哪些方法,但不需要定義這些方法的具體內容,我們可以通過interface來定義一個接口,就像定義一個標準的類一樣,但其中定義所有的方法都是空的.

接口中定義的所有方法都必須是public,這是接口的特性.

實現:要實現一個接口,可以使用implements操作符,類中必須實現接口中定義的所有方法,否則會報一個fatal錯誤,如果要實現多個接口,可以用逗號來分隔多個接口的名稱.

Note:實現多個接口時,接口中的方法不能有重名.

Note:接口也可以繼承,通過使用extends操作符.

常量:接口中也可以定義常量,接口常量和類常量的使用完全相同,它們都是定值,不能被子類或子接口修改.

Example #1 接口代碼示例:

  1. <?php 
  2.  
  3. // 聲明一個'iTemplate'接口 
  4. interface iTemplate 
  5.    public function setVariable($name$var); 
  6.    public function getHtml($template); 
  7.  
  8.  
  9. // 實現接口 
  10. // 下面的寫法是正確的 
  11. class Template implements iTemplate 
  12.    private $vars = array(); 
  13.  
  14.    public function setVariable($name$var
  15.    { 
  16.        $this->vars[$name] = $var
  17.    } 
  18.  
  19.    public function getHtml($template
  20.    { 
  21.        foreach($this->vars as $name => $value) { 
  22.            $template = str_replace('{' . $name . '}'$value$template); 
  23.        } 
  24.  
  25.        return $template
  26.    } 
  27.  
  28. // 下面的寫法是錯誤的,會報錯: 
  29. // Fatal error: Class BadTemplate contains 1 abstract methods 
  30. // and must therefore be declared abstract (iTemplate::getHtml) 
  31. class BadTemplate implements iTemplate 
  32. {  //開源軟件:Vevb.com 
  33.    private $vars = array(); 
  34.  
  35.    public function setVariable($name$var
  36.    { 
  37.        $this->vars[$name] = $var
  38.    } 
  39. ?> 

Example #2 Extendable Interfaces

  1. <?php 
  2. interface a 
  3.    public function foo(); 
  4.  
  5. interface b extends a 
  6.    public function baz(Baz $baz); 
  7.  
  8. // 正確寫法 
  9. class c implements b 
  10.    public function foo() 
  11.    { 
  12.    } 
  13.  
  14.    public function baz(Baz $baz
  15.    { 
  16.    } 
  17.  
  18. // 錯誤寫法會導致一個fatal error 
  19. class d implements b 
  20.    public function foo() 
  21.    { 
  22.    } 
  23.  
  24.    public function baz(Foo $foo
  25.    { 
  26.    } 
  27. ?> 

Example #3 多個接口間的繼承

  1. <?php 
  2. interface a 
  3.    public function foo(); 
  4.  
  5. interface b 
  6.    public function bar(); 
  7.  
  8. interface c extends a, b 
  9.    public function baz(); 
  10.  
  11. class d implements c 
  12.    public function foo() 
  13.    { 
  14.    } 
  15.  
  16.    public function bar() 
  17.    { 
  18.    } 
  19.  
  20.    public function baz() 
  21.    { 
  22.    } 
  23. ?> 

Example #4 使用接口常量

  1. <?php 
  2. interface a 
  3.    const b = 'Interface constant'
  4.  
  5. // 輸出接口常量 
  6. echo a::b; 
  7.  
  8. // 錯誤寫法,因為常量的值不能被修改。接口常量的概念和類常量是一樣的。 
  9. class b implements a 
  10.    const b = 'Class constant'
  11. ?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 太仓市| 南丹县| 西林县| 衡南县| 英超| 车险| 温宿县| 通辽市| 出国| 霸州市| 天峻县| 固阳县| 白朗县| 盈江县| 竹山县| 柘城县| 潢川县| 遂昌县| 行唐县| 博野县| 区。| 徐汇区| 无极县| 梁河县| 南雄市| 佳木斯市| 三明市| 大渡口区| 辽源市| 安化县| 兰州市| 泸溪县| 青河县| 湄潭县| 当雄县| 海门市| 宣恩县| 溧阳市| 天等县| 桃园县| 全椒县|