PHP中的Abstract Class和Interface

Abstract Class

什麼是Abstract Class( 抽象類)

和C++中的抽象類概念同樣,包含有純虛函數(Java和Php中叫abstract method)的類叫作Abstract Class。 咱們有時候也把abstract Class叫作base class,由於base class不能直接生成對象。php

PHP 中的abstract Class

咱們來看代碼: html

1 abstract class abc
2 {
3   public function xyz()
4   {
5     return 1;
6   }
7 }
8 $a = new abc();//this will throw error in php

 

PHP中的abstract class和其餘 oop語言同樣,咱們用關鍵字  abstract 來聲明一個抽象類,若是你想直接生成這個類的對象,會報錯。

 

 1 abstract class testParent
 2 {
 3     public function abc()
 4     {
 5       //body of your funciton
 6   }
 7 }
8 class testChild extends testParent 9 { 10   public function xyz() 11   { 12     //body of your function 13   } 14 }
15 $a = new testChild();
testChild經過關鍵字extends 繼承了抽象類 testParent, 而後咱們就能夠生成 一個testChild的對象了。

實現Abstract Method

相似於C++中的純虛函數,咱們只能在抽象類中申明Abstract method,而只能並且必須在子類中定義它。less

其實這種說法不是絕對的,可是爲了方便記憶,大多說的教材這樣說,來咱們回顧一下Effective C++中的關於pure virtual函數的講解。函數

「Pure Virtual函數必須在derived class中從新聲明,可是它們也能夠有本身的實現」oop

class Airplane{
public:
    virtual void fly(const Airport& destination) = 0;
    ....
};
 
void Airplane::fly(const Airport& destination){
    // 缺省行爲,將飛機飛到指定的目的地
}
class ModelA: public Airplane{
public:
    virtual void fly(const Airport& destination)
    {Airplane::fly(destination);}
    ....
};
 
class ModelB: public Airplane{
public:
    virtual void fly(const Airport& destination);
    ....
};
void ModelB:: fly(const Airport& destination){
    // 將C型飛機飛到指定的地方
}

 

實際上,咱們在 derived class ModelA中對virtual method作了一個 inline調用

 

想在的fly被分割爲兩個基本要素:this

其聲明部分表現的是接口(這個derived class必須使用的)spa

其定義部分則變現出缺省行爲(那是derived class可能使用的,但只有在它們明確提出申請時纔是)翻譯


以上內容摘自《Effective C++改善程序與設計的55個具體作法 》條款34: 區分接口繼承和實現繼承設計

 

讓咱們回來繼續討論PHP中的abstract method的實現。rest

 

abstract class abc
{
  abstract protected function f1($a , $b);
}
class xyz extends abc
{
  protected function f1($name , $address)
  {
    echo $name , $address;
  }
}
$a = new xyz();

在abc中,咱們用關鍵字abstract 聲明瞭一個abstract method f1。在PHP中

 

一旦你在abstract class中聲明瞭一個abstract method,那麼全部繼承這個class的subclass都必需要去declare這個method,不然,php會報錯。

abstract class parentTest
{
  abstract protected function f1();
  abstract public function f2();
  //abstract private function f3(); //this will trhow error
}
class childTest {   public function f1()   {     //body of your function   }   public function f2()   {     //body of your function   }   protected function f3()   {     //body of your function   } }
$a = new childTest();

上面的代碼中能夠看到,申明一個private的abstract method將會報錯,由於private method只能在當前的類中使用。

注意到在abstract class中 f1函數是protected,可是在subclass中咱們能夠將其聲明爲public的。no any visibility is less restricted than public.

Interface

Interface in oop enforce definition of some set of method in the class。

interface將會強迫用戶去實現一些method。例若有一個class中必需要求set ID和Name這兩個屬性,那麼咱們就能夠把這個class申明爲interface,這樣全部繼承自這個class的derived class都將強制必須實現setId和setName兩個操做

 

Interface in php

Interface abc
{
    public function xyz($b);
}

和其餘oop語言同樣,咱們用關鍵字 Interface 來聲明。

在這個interface裏面咱們聲明瞭一個method xyz,則任什麼時候候,subclass中都必須申明這樣的一個method xyz

class test implements abc
{
  public function xyz($b)
  {
    //your function body
  }
}

 

你能夠用關鍵字  implements 來繼承自interface。 

在interface中,只能使用public,而不能使用諸如protected和private 

interface template1
{
  public function f1();
}
interface template2 extends template1
{
  public function f2();
}
class abc implements template2
{
  public function f1()
  {
    //Your function body
  }
  public function f2()
  {
    //your function body
  }
}
你能夠用 extends關鍵字來繼承interface,好像class那樣。 

這裏的template2將包含全部template1的屬性,所以在implements自template2的class abc,將必須實現 function f1和f2,

你還能夠extends multiple interface:

 1 interface template1
 2 {
 3   public function f1();
 4 }
 5 interface template2
 6 {
 7   public function f2();
 8 }
 9 interface template3 extends template1, template2
10 {
11   public function f3();
12 }
13 class test implements template3
14 {
15   public function f1()
16   {
17     //your function body
18   }
19   public function f2()
20   {
21     //your function body
22   }
23   public function f3()
24   {
25     //your function body
26   }
27 }

 

同時,你的class也能夠implements多個interface

 

 1 interface template1
 2 {
 3   public function f1();
 4 }
 5 interface template2
 6 {
 7   public function f2();
 8 }
 9 class test implments template1, template2
10 {
11   public function f1()
12   {
13     //your function body
14   }
15   public function f2()
16   {
17     //your function body
18   }
19 }

可是若是兩個interface包含一樣名字的method,那麼你的class將不能同時implement他們。

  

繼承自interface中的method必須有一樣的參數規範,例以下面的代碼是可行的:

 

interface template1
{
  public function f1($a)
}
class test implements template1
{
  public function f1($a)
  {
    echo $a;
  }
}

 


可是這樣的代碼將會出錯:
interface template1
{
  public function f1($a)
}
class test implements template1
{
  public function f1()
  {
    echo $a;
  }
}

 

可是,咱們不須要把兩個method裏面的參數命名爲一樣的名稱,下面的代碼是可行的:
interface template1
{
  public function f1($a)
}
class test implements template1 {   public function f1($name)   {     echo $name;   } }

 

同時,若是使用default value,你還能夠改變參數的default value,下面的代碼是可行的:

 

interface template1
{
  public function f1($a = 20)
}
class test implements template1 {   public function f1($name = ankur)   {     echo $name;   } }

 

Abstract Class和Interface之間的不一樣:

1. In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract.

在Abstract class中並不是全部的method都必須是抽象的,可是在interface中全部的method都自動成爲抽象的。就是在子類中必須聲明和實現

2. Multiple and multilevel both type of inheritance is possible in interface. But single and multilevel inheritance is possible in abstract classes.

multiple和multilevel inheritance,我不知道改怎麼翻譯更好,multiple inheritance意思是 在interface中,一個class能夠同時implements好多個interface;可是在abstract classes中,只能extends一個class。

固然你extends的這個class可能又extentds別的class,這就是所謂的multilevel inheritance。

3. Method of php interface must be public only. Method in abstract class in php could be public or protected both.

interface中的method必須是public的,可是在abstract class中能夠是public或者protected。

4. In abstract class you can define as well as declare methods. But in interface you can only defined your methods.

在abstract class中你能夠同時聲明(declare)和定義(define)methodes,可是在interface中你只能定義那個methods

  

轉自:https://www.2cto.com/kf/201505/398131.html

相關文章
相關標籤/搜索