原文:
最近开始学习PHP+MySQL,记录下学习过程中的重点内容吧,然后考虑把开发网站的过程也写一个系列Blog。
这篇blog主要介绍了Abstract Class和Interface的区别。
Abstract Class什么是Abstract Class( 抽象类)
和C++中的抽象类概念一样,包含有纯虚函数(Java和Php中叫abstract method)的类叫做Abstract Class。 我们有时候也把abstract Class叫做base class,因为base class不能直接生成对象。
PHP 中的abstract Class
我们来看代码:
abstract class abc{public function xyz(){return 1;}}$a = new abc();//this will throw error in php
PHP中的abstract class和其他oop语言一样,我们用关键字 abstract 来声明一个抽象类,,如果你想直接生成这个类的对象,会报错。
abstract class testParent{public function abc(){//body of your funciton}}class testChild extends testParent{public function xyz(){//body of your function}}$a = new testChild();
testChild通过关键字extends 继承了抽象类 testParent, 然后我们就可以生成 一个testChild的对象了。实现Abstract Method
类似于C++中的纯虚函数,我们只能在抽象类中申明Abstract method,而只能而且必须在子类中定义它。
其实这种说法不是绝对的,但是为了方便记忆,大多说的教材这样说,来我们回顾一下Effective C++中的关于pure virtual函数的讲解。
“Pure Virtual函数必须在derived class中重新声明,但是它们也可以有自己的实现”
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被分割为两个基本要素:
其声明部分表现的是接口(这个derived class必须使用的)
其定义部分则变现出缺省行为(那是derived class可能使用的,但只有在它们明确提出申请时才是)
以上内容摘自《Effective C++改善程序与设计的55个具体做法 》条款34: 区分接口继承和实现继承
让我们回来继续讨论PHP中的abstract method的实现。
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 anyvisibilityis less restricted than public.
Interface
Interface in oop enforcedefinitionof some set of method in the class。
interface将会强迫用户去实现一些method。例如有一个class中必须要求set ID和Name这两个属性,那么我们就可以把这个class申明为interface,这样所有继承自这个class的derived class都将强制必须实现setId和setName两个操作
Interface in phpInterface 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,
等待故人的归来。山上的树,大多数是松树比较突出。