接口允许您创建定义实现接口的类的方法的代码。但是,您不能向这些方法添加任何代码。
抽象类允许您做同样的事情,同时向方法添加代码。
现在,如果可以用抽象类实现同样的目标,我们为什么还需要接口的概念呢?
有人告诉我,这与从c++到Java的面向对象理论有关,这也是PHP面向对象的基础。这个概念在Java中有用而在PHP中没有吗?这仅仅是一种避免在抽象类中布满占位符的方法吗?我遗漏了什么吗?
接口允许您创建定义实现接口的类的方法的代码。但是,您不能向这些方法添加任何代码。
抽象类允许您做同样的事情,同时向方法添加代码。
现在,如果可以用抽象类实现同样的目标,我们为什么还需要接口的概念呢?
有人告诉我,这与从c++到Java的面向对象理论有关,这也是PHP面向对象的基础。这个概念在Java中有用而在PHP中没有吗?这仅仅是一种避免在抽象类中布满占位符的方法吗?我遗漏了什么吗?
当前回答
接口的全部意义在于为您提供灵活性,使您的类强制实现多个接口,但仍然不允许多重继承。从多个类继承的问题很多,维基百科的页面很好地总结了这些问题。
接口是一种折衷。关于多重继承的大多数问题并不适用于抽象基类,所以现在大多数现代语言禁用多重继承,但却调用抽象基类接口,并允许一个类“实现”任意数量的抽象基类。
其他回答
我们看到抽象类和接口的相似之处在于它们提供了必须在子类中实现的抽象方法。但是,二者仍有以下差异:
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
希望这将有助于任何人理解!
这个概念在面向对象编程中非常有用。对我来说,我认为接口是一种契约。只要我的类和你的类同意这个方法签名合同,我们就可以“接口”。至于抽象类,我认为更多的是存根一些方法的基类,我需要填充细节。
接口不会给您的代码带来任何性能提升或类似的东西,但它们可以使代码更易于维护。的确,抽象类(甚至非抽象类)可以用来建立到代码的接口,但是适当的接口(用关键字定义并且只包含方法签名的接口)更容易排序和读取。
话虽如此,在决定是否使用接口而不是类时,我倾向于谨慎。有时我想要默认的方法实现,或者所有子类都通用的变量。
当然,关于多接口实现的观点也是合理的。如果您有一个实现多个接口的类,您可以在同一个应用程序中使用该类的对象作为不同的类型。
The fact that your question is about PHP, though, makes things a bit more interesting. Typing to interfaces is still not incredibly necessary in PHP, where you can pretty much feed anything to any method, regardless of its type. You can statically type method parameters, but some of that is broken (String, I believe, causes some hiccups). Couple this with the fact that you can't type most other references, and there isn't much value in trying to force static typing in PHP (at this point). And because of that, the value of interfaces in PHP, at this point is far less than it is in more strongly-typed languages. They have the benefit of readability, but little else. Multiple-implementation isn't even beneficial, because you still have to declare the methods and give them bodies within the implementor.
界面就像你的基因。
抽象类就像你实际的父母。
它们的目的是遗传的,但是在抽象类和接口的情况下,继承的是更具体的。
下面是PHP接口的要点
It is used to define required no of methods in class [if you want to load html then id and name is required so in this case interface include setID and setName]. Interface strictly force class to include all the methods define in it. You can only define method in interface with public accessibility. You can also extend interface like class. You can extend interface in php using extends keyword. Extend multiple interface. You can not implement 2 interfaces if both share function with same name. It will throw error.
示例代码:
interface test{
public function A($i);
public function B($j = 20);
}
class xyz implements test{
public function A($a){
echo "CLASS A Value is ".$a;
}
public function B($b){
echo "CLASS B Value is ".$b;
}
}
$x = new xyz();
echo $x->A(11);
echo "<br/>";
echo $x->B(10);