接口允许您创建定义实现接口的类的方法的代码。但是,您不能向这些方法添加任何代码。

抽象类允许您做同样的事情,同时向方法添加代码。

现在,如果可以用抽象类实现同样的目标,我们为什么还需要接口的概念呢?

有人告诉我,这与从c++到Java的面向对象理论有关,这也是PHP面向对象的基础。这个概念在Java中有用而在PHP中没有吗?这仅仅是一种避免在抽象类中布满占位符的方法吗?我遗漏了什么吗?


当前回答

接口不仅仅是为了确保开发人员实现特定的方法。其思想是,因为这些类保证有特定的方法,即使不知道类的实际类型,也可以使用这些方法。例子:

interface Readable {
  String read();
}

List<Readable> readables; // dunno what these actually are, but we know they have read();
for(Readable reader : readables)
  System.out.println(reader.read());

在许多情况下,提供基类是没有意义的,不管是不是抽象的,因为实现变化很大,除了一些方法之外没有任何共同之处。

动态类型语言有“鸭子类型”的概念,你不需要接口;您可以自由地假设对象具有您正在对其调用的方法。在静态类型语言中,对象有一些方法(在我的例子中是read()),但没有实现接口,这种方法可以解决这个问题。

其他回答

如果已经有抽象类了,为什么还需要接口呢? 防止多重继承(可能导致多个已知问题)。

这类问题之一:

“钻石问题”(有时被称为“致命的钻石”) 死亡”)是当两个类B和C继承时产生的模糊性 类D继承自类B和类c B和C已经覆盖了A, D没有覆盖它,那么 D继承了哪个版本的方法:B的还是C的?

来源:https://en.wikipedia.org/wiki/Multiple_inheritance The_diamond_problem

Why/When to use an interface? An example... All cars in the world have the same interface (methods)... AccelerationPedalIsOnTheRight(), BrakePedalISOnTheLeft(). Imagine that each car brand would have these "methods" different from another brand. BMW would have The brakes on the right side, and Honda would have brakes on the left side of the wheel. People would have to learn how these "methods" work every time they would buy a different brand of car. That's why it's a good idea to have the same interface in multiple "places."

界面为你做了什么(为什么有人会使用它)? 接口可以防止你犯“错误”(它保证所有实现特定接口的类都有接口中的方法)。

// Methods inside this interface must be implemented in all classes which implement this interface.
interface IPersonService
{   
    public function Create($personObject);
}

class MySqlPerson implements IPersonService
{
    public function Create($personObject)
    {
        // Create a new person in MySql database.
    }
}

class MongoPerson implements IPersonService
{
    public function Create($personObject)
    {
        // Mongo database creates a new person differently then MySQL does. But the code outside of this method doesn't care how a person will be added to the database, all it has to know is that the method Create() has 1 parameter (the person object).
    }
}

这样,Create()方法将始终以相同的方式使用。不管我们使用的是MySqlPerson类还是MongoPerson类。我们使用方法的方式保持不变(接口保持不变)。

例如,它将像这样使用(在我们的代码中无处不在):

new MySqlPerson()->Create($personObject);
new MongoPerson()->Create($personObject);

这样,这样的事情就不会发生:

new MySqlPerson()->Create($personObject)
new MongoPerson()->Create($personsName, $personsAge);

记住一个界面并在任何地方使用相同的界面要比多个不同的界面容易得多。

这样,Create()方法的内部对于不同的类可以是不同的,而不会影响调用该方法的“外部”代码。所有外部代码必须知道的是Create()方法有1个参数($personObject),因为这是外部代码使用/调用该方法的方式。外部代码并不关心方法内部发生了什么;它只需要知道如何使用/调用它。

You can do this without an interface as well, but if you use an interface, it's "safer" (because it prevents you to make mistakes). The interface assures you that the method Create() will have the same signature (same types and a same number of parameters) in all classes that implement the interface. This way you can be sure that ANY class which implements the IPersonService interface, will have the method Create() (in this example) and will need only 1 parameter ($personObject) to get called/used.

实现接口的类必须实现接口所具有的所有方法。

我希望我没有重复太多。

我们看到抽象类和接口的相似之处在于它们提供了必须在子类中实现的抽象方法。但是,二者仍有以下差异:

1.接口可以包含抽象方法和常量,但不能包含具体方法和变量。 2.接口中的所有方法都必须在公共可见性中 范围。 3.一个类可以实现多个接口,同时它可以继承 只来自一个抽象类。

                                  interface                      abstract class
the code                     - abstract methods               - abstract methods
                             - constants                      - constants                  
                                                              - concrete methods
                                                              - concrete variables

access modifiers             
                             - public                         - public
                                                              - protected
                                                              - private
                                                                etc.
number of parents          The same class can implement
                           more than 1 interface              The child class can 
                                                              inherit only from 1 abstract class

希望这将有助于任何人理解!

界面就像你的基因。

抽象类就像你实际的父母。

它们的目的是遗传的,但是在抽象类和接口的情况下,继承的是更具体的。

在我看来,接口应该优于非函数抽象类。如果性能受到影响,我也不会感到惊讶,因为只实例化了一个对象,而不是解析两个对象,并将它们组合在一起(尽管,我不能确定,我不熟悉OOP PHP的内部工作原理)。

的确,与Java相比,接口没有那么有用/有意义。另一方面,PHP6将引入更多的类型提示,包括返回值的类型提示。这将为PHP接口增加一些价值。

interfaces定义了需要遵循的方法列表(想想API),而抽象类提供了一些基本/通用的功能,这些功能由子类细化以满足特定需求。

接口的全部意义在于为您提供灵活性,使您的类强制实现多个接口,但仍然不允许多重继承。从多个类继承的问题很多,维基百科的页面很好地总结了这些问题。

接口是一种折衷。关于多重继承的大多数问题并不适用于抽象基类,所以现在大多数现代语言禁用多重继承,但却调用抽象基类接口,并允许一个类“实现”任意数量的抽象基类。