60

我已经在 SO 以及网络的其余部分搜索了一个好的答案,但我还没有找到一个我真正理解的答案。我将以不同的方式呈现这一点,希望答案也能帮助其他人。

据我了解,这两个概念具有相同的规则,只是抽象类由于方法实现能力而更加灵活。另外,我知道您可以实现多个接口并且只扩展一个类,但我确信与我提到的两个相比存在更多差异。

请查看这两个代码片段,并给我一个示例,我可以对我的每个示例做什么,这会让我想要或不想使用另一个。

抽象类

abstract class Foo {
    abstract public function getValue();
    abstract public function setValue($value); 
}


class myObj extends Foo {
    function getValue() {

    }
    function setValue($value) {

    }
}

界面

interface Foo {
    public function getValue();
    public function setValue($value);
}

class myObj implements Foo {
    function getValue() {

    }
    function setValue($value) {

    }
}
4

4 回答 4

155

抽象的

抽象类关注一种事物的相似性。

人们被认为是类型mammal,因此不会被认为是类型vehicle

界面

接口侧重于相似功能的整理。

例如:您是人类,属于mammal. 如果你想飞,那么你需要实现一个flying Interface. 如果你想在飞行中射击,那么你还需要实现gun Interface.

请参阅以下示例:

abstract class Mammal {
      protected $age_;
      //below are functions I think all mammals will have,including people
      abstract public function setAge($age);
      abstract public function getAge();
      abstract public function eat($food);
}
class Person extends Mammal {
      protected $job_; //Person's feature
      public function setAge($age){
        $this->age_ = $age;
      }

      public function getAge(){
        return $this->age_;
      }

      public function eat($food){
        echo 'I eat ' ,$food ,'today';
      }

      //People only attribute
      public function setJob($job){
         $this->job_ = $job;
      }
      public function getJob(){
         echo 'My job is ' , $this->job_;
      }

}

//Now a person wants to fly, but they are typically not able to do so.
//So we implement an interface
interface Plane{
  public function Fly(); 
}

//I also want shoot enemy
interface Gun{
  public function shoot();
}

class Person2 extends Mammal implements Plane,Gun{

      protected $job_;//Person feature
      public function setAge($age){
        $this->age_ = $age;
      }
      public function getAge(){
        return $this->age_;
      }
      public function eat($food){
        echo '<br/>I eat ' ,$food ,' today<br/>';
      }
      //Only a person has this feature.
      public function setJob($job){
         $this->job_ = $job;
      }
      public function getJob(){
         echo 'My job is ' , $this->job_;
      }

      //-----------------------------------------
      //below implementations from interfaces function. (features that humans do not have).
      //Person implements from other class
      public function fly(){
        echo '<br/>I use plane,so I can fly<br/>';
      }
      public function shoot(){
        echo 'I use gun,so I can shoot<br/>';
      }
}

$People = new Person();
echo '<pre>';
print_r( get_class_methods('People'));
echo '</pre>';

echo '<pre>';
print_r( get_class_methods('People2'));
echo '</pre>';

$People2 = new Person2();
$People2->setAge(24);
echo $People2->getAge();
$People2->eat('egg');
$People2->setJob('PHP devepop');
echo $People2->getJob();

$People2->fly();
$People2->shoot();
于 2013-04-12T01:08:59.090 回答
31

To resume the idea (globally, not in detail):

inheritance

is the notion to extend from something, and optionally add some new feature or override some existing feature (to do differently). But using inheritance, you share a big part of code with the parent. You are a parent + some other things.

interface

is representing some abilities (we says a class is implementing an interface to says that it has these abilities). An interface can be implemented by 2 classes which are completely different and do not share their code (except for methods they implements). When A and B are implementing interface C, A is not a B and B is not a A.

And one of the reason for interface is indeed to allow programmer to do the same as they could do with multi-inheritance, but without multi-inheritance problems.

This notion is used in some programming languages like JAVA, PHP...

于 2013-04-11T23:53:08.230 回答
25

简单来说,接口就是规范一组函数,抽象类就是定义一个基本的骨架,供类派生。

于 2013-04-15T05:57:52.107 回答
3

I have thought about this before, and the best I could conclude was that interfaces are a logically convenient abstraction of a pure abstract class (c++).

As for why you would choose interfaces over abstract classes, I quote (a c++ source but the concepts are the same):

Note that there is a great temptation to add concrete member functions and data to pure abstract base classes. This must be resisted, in general it is a sign that the interface is not well factored. Data and concrete member functions tend to imply a particular implementation and as such can inherit from the interface but should not be that interface. Instead if there is some commonality between concrete classes, creation of abstract class which inherits its interface from the pure abstract class and defines the common data and member functions of the concrete classes works well.

The thing is, when using interfaces, the first thing that comes to mind is decoupling. When using an interface, the user and the implementing-class are totally decoupled. The same applies for when you're using a pure abstract class which is basically an interface.

于 2013-04-11T23:55:15.327 回答