[英]If I call an interface method, will it get the method body from implementation class and execute?
I have an interface Interface1
. 我有一个接口
Interface1
。 I have its implementation Imple implements Interface1
(all methods have been implemented :) ). 我有它的实现
Imple implements Interface1
(所有方法都已实现:))。
Now, consider a third class CheckCall
, can I do a call in the class CheckCall
like I mention below : 现在,考虑第三类
CheckCall
,我可以在类CheckCall
进行调用,如下所述:
Interface1 interface1;
interface1.method();
All necessary imports have been done. 所有必要的进口都已完成。 Please tell me is it possible or not , if not then ok and if yes, then tell me what will happen if I've more than one implementing classes for the same interface and I'm doing the same call.
请告诉我是否可能,如果不是那么好,如果是,那么告诉我如果我有多个实现类用于同一个接口并且我正在进行相同的调用会发生什么。
Well, you cannot call the method directly on the interface, but you can do what you wrote, more or less. 好吧,你不能直接在界面上调用方法,但你可以或多或少地做你写的。
You wrote: 你写了:
Interface1 interface1;
interface1.method();
This will work if you do this: 如果您这样做,这将有效:
Interface1 interface1 = new CheckCall();
interface1.method();
then tell me what will happen if i have more than one impl classes for the same interface and i am doing the same call
然后告诉我如果我有同一个接口的多个impl类并且我正在进行相同的调用会发生什么
Well, that's the nice thing about Java: the problem you're referring to is called the "diamond problem": 嗯,这是关于Java的好处:你所指的问题叫做“钻石问题”:
http://en.wikipedia.org/wiki/Diamond_problem http://en.wikipedia.org/wiki/Diamond_problem
And it doesn't really exist in Java because Java fully supports multiple inheritance, but only through "multiple (Java) interface inheritance" (* see comment). 它并不存在于Java中,因为Java完全支持多重继承,但只能通过“多(Java)接口继承” (*参见注释)。
So in your case when you call interface1.method()
you're either calling Impl
's method or CheckCall
's method and there's no confusion possible. 因此,在您调用
interface1.method()
情况下,您要么调用Impl
的方法,要么调用CheckCall
的方法,并且不存在混淆。
Sure, your code works fine! 当然,你的代码工作正常! You just have to initialize your variable
interface1
with an actual implementation (ie new Imple()). 您只需使用实际实现(即new Imple())初始化变量
interface1
。
Check out this example, I used your class-names: 看看这个例子,我使用了你的类名:
public class CheckCall {
interface Interface1 {
void method();
}
static class Imple implements Interface1 {
@Override
public void method() {
System.out.println("Imple.method1()");
}
}
public static void main(String[] args) {
Interface1 interface1;
interface1 = new Imple();
interface1.method();
}
}
Yes, but not as you have it written. 是的,但不是因为你写的。 You'd have to say:
你不得不说:
Interface1 interface1 = new Imple();
You can create a variable of type Interface, and then instantiate it to the class that implements the interface. 您可以创建一个Interface类型的变量,然后将其实例化为实现该接口的类。 You see this quite often with the Java collections, for example:
您经常使用Java集合看到这一点,例如:
List<String> a = new ArrayList<String>();
Actually, you should refer to methods by the interfaces that define them, but you need an actual implementing class. 实际上,您应该通过定义它们的接口来引用方法,但是您需要一个实际的实现类。 So I would usually do it like this:
所以我通常会这样做:
// the variable is of type Interface1
Interface1 interface1 = new Imple();
// but the method will be executed on the Imple object
interface1.method();
No, you need to make the call on the object that implements the interface, not on the interface itself. 不,您需要对实现接口的对象进行调用,而不是在接口本身上进行调用。
Edit: A variable with that type could be used but it would still need to be an class that implements that type. 编辑:可以使用具有该类型的变量,但它仍然需要是实现该类型的类。 You can't create an instance of an interface.
您无法创建接口的实例。
No. 没有。
As you can't instanciate an interface, you'll have to create a Imple
object, to use it as an Interface1
one like this : 由于您无法实现接口,因此您必须创建一个
Imple
对象,将其用作Interface1
如下所示:
Interface1 interface1 = new Imple();
interface1.method();
in fact, an interface main interest come from the ability to have a method return any object implementing it, without having to worry about given implementation. 实际上,接口的主要兴趣来自于让方法返回实现它的任何对象的能力,而不必担心给定的实现。 in your case, it could shows up as
在你的情况下,它可以显示为
public class CheckCall {
public Interface1 factoryMethod() {
return new Imple();
}
public void test() {
Interface1 used = factoryMethod();
used.method();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.