和C++中的抽象类概念同样,包含有纯虚函数(Java和Php中叫abstract method)的类叫作Abstract Class。 咱们有时候也把abstract Class叫作base class,由于base class不能直接生成对象。php
咱们来看代码: 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();
相似于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 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 abc { public function xyz($b); }
在这个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 } }
这里的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; } }
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