OOP接口点
可能的重复:
接口与抽象类(一般面向对象)
编辑: 我刚刚阅读了“可能重复”中的问题和问题的答案,有人认为这两个问题甚至很相似,我感到非常难过...但是,哦,好吧...
--------- -------------------------------------------------- --------------
大家好, 我试图了解 OOP 范式中的接口。 我知道抽象类和接口之间的区别,我也知道接口基本上允许简单的多重继承行为和设计,但我不明白的是“承诺原则”。我的意思是,接口应该是一个承诺,即实现接口的类已实现所有接口方法。
我不明白的是,每次调用类的方法时,我们是否都必须检查类是否实现了带有 instanceOf 的接口?如果不阅读文档,您不知道某个类实现了接口。如果您阅读代码,您可以看到自己定义了该方法并且可以调用它?!
如果我有
情况 A.
class Ball{
function kick(){...};
}
或 情况 B
interface Kickable{
function kick;
}
class Ball implements Kickable{
function kick(){...};
}
唯一的区别是,在情况 A 中,当调用一个不存在的方法(“运行时”)时,我会收到一个错误,而在情况 B 中,当我尝试运行代码时,我会收到此错误尝试“编译”。这里运行时和编译肯定用错了(PHP环境)。
我记得Java中有一个Runnable接口可以启用线程。为什么我们必须实现一个 Runnable 接口,然后在该类中定义 run() 方法?我的意思是,类可以有一个 Run 方法而无需实现接口,并且有方法检查类是否定义了特殊方法。好吧,也许我的问题的 Java 部分有点令人困惑:)))
我很抱歉提出了这样一个令人困惑的问题,但我希望有人能够理解这些问题,现在他可以分享他的结论:)
谢谢, 卢卡
Possible Duplicate:
Interface vs Abstract Class (general OO)
EDIT:
I just read the questions and answers to the questions from "possible duplicate" and I feel really sad that someone considers these two questions even similar... but, oh well...
-------------------------------------------------------------------------
Hello everyone,
I am trying to understand something about Interfaces in OOP paradigm.
I know the difference between abstract class and interface, I also know that interfaces basically allow easy multiple inheritance behaviour and design, but what I don't get is the "principle of promise". I mean, interface should be a promise that a class implementing an interface has all interface methods implemented.
What I don't understand is do we have to check if class implements interface with instanceOf every time we call its methods? Without reading documentation you have no idea some class implements interface. And if you read the code than you can see yourself that there is that method defined and you can call it?!
If I have
case A.
class Ball{
function kick(){...};
}
or
case B.
interface Kickable{
function kick;
}
class Ball implements Kickable{
function kick(){...};
}
the only difference is that in case A I'll get an error when calling a method that it doesn't exist ("in runtime") and in case B I'll get this error when trying to run the code while trying to "compile". Runtime and compile are definitely used wrong here (PHP environment).
I remember in Java there was a Runnable interface which enables threading. Why do we have to implement an interface Runnable and then define run() method in that class? I mean, class could have a Run method without implementing an interface and there are means to check if class has a special method defined. Ok, maybe my Java part of question is a bit confusing :)))
I'm sorry for such a confusing question, but I hope someone went through these problems in understanding and that now he can share his conclusion :)
Thanks,
Luka
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已经在问题中提到了接口的大部分好处,即:
您还提到您知道抽象类和接口之间的区别。使用接口的另一个好处是:
这基本上是上面第一点的重新哈希,但它把它放在一个你可能没有的角度之前考虑过。以 Java Runnable 为例:如果 Runnable 是一个抽象类,那么任何实现线程的类都需要从它继承。这最终会导致代码极其不灵活,因为您无法从任何其他基类继承。但是,由于 Runnable 是一个接口,因此您可以让任何类实现它(无论它继承自哪个基类)。
我理解您对必须检查一个类是否实现接口的担忧 - 不幸的是,在弱类型语言中您必须这样做,特别是因为 PHP 类型提示尚未完全成熟。
在强类型语言(如 Java)中,您通常不会有这样的担忧,因为如果您在未实现接口(或未实现具体方法)。
You've already named most of the benefits of interfaces in your question, namely:
You also mention that you know the difference between abstract classes and interfaces. Therein lies another benefit of using interfaces:
This is basically a re-hash of the first point above, but it puts it in a perspective that you might not have considered before. Take your Java Runnable example: If Runnable was an abstract class, then any and every class that implements threading would need to inherit from it. That would lead to extremely inflexible code in the end, as you'd not be able to inherit from any other base class. However, since Runnable is an interface, you can have any class implement it (regardless of what base class it may inherit from).
I understand your concern about having to check if a class implements an interface - unfortunately in a weakly typed language you will have to do that, especially since PHP type hinting hasn't totally come into its own yet.
In a strongly typed language, like Java, you generally don't have such concerns, as you will get a compile-time error if you call an interface method on a class that doesn't implement the interface (or doesn't implement the specific method).
不,你不必使用instanceof。这是为了运行时类型检查。
如果您想确保您使用的类实现了该接口,只需将接口类型放入您的方法签名中即可。例如
,然后您可以做一些其他好事,这取决于您的语言。例如,使用java,您可以强制泛型类型实现给定的接口,从而允许泛型编程:
接口的原因主要有2:
No. You haven't to use instanceof. That's for run-time type checking.
If you want to ensure that you are using a class that implements that interface simply put the interface type in your method signature. For example
Then some other nice things you can do, depends on your language. For example with java you can force a generic type to implement a given interface, allowing generic programming:
The reason of interfaces are mainly 2:
我认为你误解了这一部分。接口允许您确保特定的类具有一组属性/方法。
示例:
对比:
I think you misunderstood that part. Interfaces allow you to ensure that a particular class has a set of properties/methods.
Example:
vs: