抽象类和接口的区别
抽象类:
- 不能直接实例化一个抽象类。(It is not allowed to create an instance of a class that has been defined as abstract.)
- 只要包含哪怕只有一个抽象方法的类,必须定义为抽象类。(Any class that contains at least one abstract method must also be abstract.)
- 抽象类内的抽象方法只需要声明访问权限(public/protected/privite),不需要定义具体实现。(Methods defined as abstract simply declare the method's signature they cannot define the implementation.)
- 继承的子类必须实现父抽象类内的所有抽象方法,并且访问权限必须等于或弱于父类中的访问权限。比如:父抽象类中定义个protected抽象方法,那么继承的子类内可为protected或public;若父抽象类中定义一个public的抽象方法,那么继承的子类内就只能为public了,不能比父类的public权限更多了。(When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or weaker) visibillity. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public. )
Example:
<?php
abstract class AbstractClass
{
// Force Extending class to define this method
abstract protected function getValue();
abstract protected function prefixValue($prefix);
// Common method
public function printOut() {
print $this->getValue() . "\n";
}
}
class ConcreteClass1 extends AbstractClass
{
protected function getValue() {
return "ConcreteClass1";
}
public function prefixValue($prefix) {
return "{$prefix}ConcreteClass1";
}
}
class ConcreteClass2 extends AbstractClass
{
public function getValue() {
return "ConcreteClass2";
}
public function prefixValue($prefix) {
return "{$prefix}ConcreteClass2";
}
}
$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_') ."\n";
$class2 = new ConcreteClass2;
$class2->printOut();
echo $class2->prefixValue('FOO_') ."\n";
?>
以上输出:
ConcreteClass1
FOO_ConcreteClass1
ConcreteClass2
FOO_ConcreteClass2
[补充:] 抽象类内的抽象方法是不能定义为私有的(private)。
接口:
- 接口对象常用于设定某类中必须要实现的方法,但是不必实现这些方法。(Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.
- 接口使用”interface“关键字定义,但是都不必定义方法的实现。(Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.)
- 所有方法必须是共有的(public)。All methods declared in an interface must be public, this is the nature of an interface.
Example:
<?php
// Declare the interface 'iTemplate'
interface iTemplate
{
public function setVariable($name, $var);
public function getHtml($template);
}
// Implement the interface
// This will work
class Template implements iTemplate
{
private $vars = array();
public function setVariable($name, $var)
{
$this->vars[$name] = $var;
}
public function getHtml($template)
{
foreach($this->vars as $name => $value) {
$template = str_replace('{' . $name . '}', $value, $template);
}
return $template;
}
}
// This will not work
// Fatal error: Class BadTemplate contains 1 abstract methods
// and must therefore be declared abstract (iTemplate::getHtml)
class BadTemplate implements iTemplate
{
private $vars = array();
public function setVariable($name, $var)
{
$this->vars[$name] = $var;
}
}
?>
几句话总结:
1. 接口可实现多重继承 ,抽象类不可以。
E.g: class ExtendsMoreCls extends AbsClsOne implements IFOne,IFTwo { ... }
2. 接口中定义的任何方法都不能有实现,而抽象类中可以实现非抽象的方法。
3. 接口中定义的每个方法必须在实现类中一一实现;而抽象类只要实现其中的抽象方法即可。
4. 接口不能继承于抽象类,但是抽象类可以实现接口。
添加新评论