接口允许您创建定义实现接口的类的方法的代码。但是,您不能向这些方法添加任何代码。
抽象类允许您做同样的事情,同时向方法添加代码。
现在,如果可以用抽象类实现同样的目标,我们为什么还需要接口的概念呢?
有人告诉我,这与从c++到Java的面向对象理论有关,这也是PHP面向对象的基础。这个概念在Java中有用而在PHP中没有吗?这仅仅是一种避免在抽象类中布满占位符的方法吗?我遗漏了什么吗?
接口允许您创建定义实现接口的类的方法的代码。但是,您不能向这些方法添加任何代码。
抽象类允许您做同样的事情,同时向方法添加代码。
现在,如果可以用抽象类实现同样的目标,我们为什么还需要接口的概念呢?
有人告诉我,这与从c++到Java的面向对象理论有关,这也是PHP面向对象的基础。这个概念在Java中有用而在PHP中没有吗?这仅仅是一种避免在抽象类中布满占位符的方法吗?我遗漏了什么吗?
当前回答
我不记得PHP在这方面是否有所不同,但在Java中,您可以实现多个接口,但不能继承多个抽象类。我认为PHP也是这样工作的。
在PHP中,您可以通过用逗号分隔多个接口来应用它们(我认为,我不认为这是一个干净的解决方案)。
至于多个抽象类,你可以有多个相互扩展的抽象(同样,我不完全确定,但我想我以前在某个地方见过)。唯一不能扩展的是final类。
其他回答
在我看来,接口应该优于非函数抽象类。如果性能受到影响,我也不会感到惊讶,因为只实例化了一个对象,而不是解析两个对象,并将它们组合在一起(尽管,我不能确定,我不熟悉OOP PHP的内部工作原理)。
的确,与Java相比,接口没有那么有用/有意义。另一方面,PHP6将引入更多的类型提示,包括返回值的类型提示。这将为PHP接口增加一些价值。
interfaces定义了需要遵循的方法列表(想想API),而抽象类提供了一些基本/通用的功能,这些功能由子类细化以满足特定需求。
如果已经有抽象类了,为什么还需要接口呢? 防止多重继承(可能导致多个已知问题)。
这类问题之一:
“钻石问题”(有时被称为“致命的钻石”) 死亡”)是当两个类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.
实现接口的类必须实现接口所具有的所有方法。
我希望我没有重复太多。
我不记得PHP在这方面是否有所不同,但在Java中,您可以实现多个接口,但不能继承多个抽象类。我认为PHP也是这样工作的。
在PHP中,您可以通过用逗号分隔多个接口来应用它们(我认为,我不认为这是一个干净的解决方案)。
至于多个抽象类,你可以有多个相互扩展的抽象(同样,我不完全确定,但我想我以前在某个地方见过)。唯一不能扩展的是final类。
接口的全部意义在于为您提供灵活性,使您的类强制实现多个接口,但仍然不允许多重继承。从多个类继承的问题很多,维基百科的页面很好地总结了这些问题。
接口是一种折衷。关于多重继承的大多数问题并不适用于抽象基类,所以现在大多数现代语言禁用多重继承,但却调用抽象基类接口,并允许一个类“实现”任意数量的抽象基类。
界面就像你的基因。
抽象类就像你实际的父母。
它们的目的是遗传的,但是在抽象类和接口的情况下,继承的是更具体的。