PHP中的Abstract Class和Interface

原文:http://www.techflirt.com/tutorials/oop-in-php/abstract-classes-interface.html

最近开始学习PHP+MySQL,记录下学习过程中的重点内容吧,然后考虑把开发网站的过程也写一个系列Blog。

这篇blog主要介绍了Abstract ClassInterface的区别。

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 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:

interface template1
{
public function f1();
}
interface template2
{
public function f2();
}
interface template3 extends template1, template2
{
public function f3();
}
class test implements template3
{
public function f1()
{
//your function body
}
public function f2()
{
//your function body
}
public function f3()
{
//your function body
}
}

同时,你的class也可以implements多个interface

interface template1
{
public function f1();
}
interface template2
{
public function f2();
}
class test implments template1, template2
{
public function f1()
{
//your function body
}
public function f2()
{
//your function body
}
}

但是如果两个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

时间: 2024-08-29 19:08:33

PHP中的Abstract Class和Interface的相关文章

c#中抽象类(abstract)和接口(interface)的相同点与区别

相同点: 1.都可以被继承 2.都不能被实例化 3.都可以包含方法声明 4.派生类必须实现未实现的方法 区别: 1.抽象基类可以定义字段.属性.方法实现.接口只能定义属性.索引器.事件.和方法声明,不能包含字段. 2.抽象类是一个不完整的类,需要进一步细化,而接口是一个行为规范.微软的自定义接口总是后带able字段,证明其是表述一类“我能做...” 3.接口可以被多重实现,抽象类只能被单一继承 4.抽象类更多的是定义在一系列紧密相关的类间,而接口大多数是关系疏松但都实现某一功能的类中 5.抽象类

【转】abstract class和interface的区别

在编程语言中,abstract class和interface是支持抽象类定义的两种机制.abstract class和interface之间在对于抽象类定义的支持方面具有很大的相似性,甚至可以相互替换,因此很多开发者在进行抽象类定义时对于abstract class和interface的选择显得比较随意.其实,两者之间还是有很大的区别的,对于它们的选择甚至反映出对于问题领域本质的理解.对于设计意图的理解是否正确.合理. Abstract class Interface 实例化 不能 不能 类

abstract class和interface的区别

在Java语言中,abstract class和interface是支持抽象类定义的两种机制.正是由于这两种机制的存在,才赋予了Java强大的面向对象能力.abstract class和interface之间在对于抽象类定义的支持方面具有很大的相似性,甚至可以相互替换,因此很多开发者在进行抽象类定义时对于abstract class和interface的选择显得比较随意.其实,两者之间还是有很大的区别的,对于它们的选择甚至反映出对于问题领域本质的理解.对于设计意图的理解是否正确.合理. Abst

abstract class与interface的区别与联系

1.相同点:A. 两者都是抽象类,都不能实例化.B. interface实现类及abstract class的子类都必须要实现已经声明的抽象方法. 2. 不同点:A. interface需要实现,要用implements,而abstract class需要继承,要用extends.B. 一个类可以实现多个interface,但一个类只能继承一个abstract class.C. interface强调特定功能的实现,而abstract class强调所属关系.D.尽管interface实现类及a

Java中abstract class 和 interface 的解释和他们的异同点(转)

(一)概述    在Java语言中, abstract class 和interface 是支持抽象类定义的两种机制.正是由于这两种机制的存 在,才赋予了Java强大的 面向对象能力.abstract class和interface之间在对于抽象类定义的支持方面具有 很大的相似性,甚至可以相互替换,因此很多开发者在进 行抽象类定义时对于abstract class和interface的 选择显得比较随意.其实,两者之间还是有很大的区别的,对于它们的选择甚至反映出对 于问题领域本质的理 解.对于设

在.NET 应用程序设计中如何选择Class, Abstract Class and Interface

关键字: Type– 类型 Class - 类 Abstract - 抽象的 Interface - 接口 Member - 成员 Method - 方法 Property - 属性 预备知识:在阅读本文时,您应当了解.NET编程的基本知识并且已经掌握Class, Abstract Class 和 Interface全部知识.这里我仅简单介绍一下他们的基本知识.本文的例子由C#编写.期望您对C#编程有一定的了解. 正文: 我们无法创建一个Abstract Class或Interface的实例(I

Java中抽象类和接口的区别(abstract class VS interface)

前言:本文解决的问题 什么是抽象类 抽象类的应用场景 接口可以有方法实现吗 接口和抽象类的区别 1 什么是抽象类 在声明时含有关键字abstract就是抽象类,抽象类它可以看作具体类的一个模板,继承它的子类可以从抽象类中共享一些方法(可能是不完整的)和属性. A class which is declared with the abstract keyword is known as an abstract class in Java. It can have abstract and non-

`Java`中`abstract class`与`interface`区别

abstract class Java中允许使用abstract修饰符声明方法,此时只定义方法但是不实现方法(abstract修饰的方法没有主体,只有一个签名和一个分号). 以下是abstract方法和这些方法所在的abstract类相关的规则: 只要类中有一个abstract方法,那么这个类本身就自动成为abstract,而且必须声明为abstract class,否则会导致编译错误; abstract class无法实例化; abstract类的子类必须覆盖超类的每个abstract方法,并

11)Java abstract class 和 interface

abstract class 和 interface 的区别 ? ? ?含有abstract修饰符的class即为抽象类,abstract 类不能创建实例对象.含有abstract方法的类必须定义为abstract class,abstract class类中的方法不一定是抽象的. ? ? ?abstract class类中定义抽象方法必须在具体(Concrete)子类中实现,所以,不能有抽象构造方法或抽象静态方法.如果子类没有实现抽象父类中的所有抽象方法,那么子类也必须定义为abstract类