[英]Why we can create an interface instance variable although Interface can't be instantiated?
I have following Interface declared 我有以下接口声明
public interface MyInterface {
void do_it_now();
}
The I can do 我能做的
public class MainClass{
public static void main(String[] args) {
MyInterface mainClass = new MyInterface() {
@Override
public void do_it_now() {
}
};
}
}
Now My Question regarding the above code is by definition Interface can't be instantiated Whey we are allowed have a instance variable Type of Interface in java. 现在,关于上述代码的“我的问题”是通过定义无法实例化接口。乳清我们被允许在Java中有一个实例变量“接口类型”。 What is the meaning of new MyInterface() line.
新的MyInterface()行的含义是什么。
I want to know whats going on under the hood. 我想知道引擎盖下发生了什么。
I have also gone through the post quite slimier to my question. 对于这个问题,我的帖子也比较苗条。 but answer is not quite satisfactory to me .
但是答案对我来说并不令人满意。
Please don't give negative feedback or block my account if you found my question stupid post comment I will remove it. 如果您发现我的问题愚蠢的帖子评论,请不要给予负面反馈或阻止我的帐户,我们将其删除。
Being able to have a variable of an interface type allows you to assign to that variable an instance of any class that implements that interface. 能够具有接口类型的变量,使您可以向该变量分配实现该接口的任何类的实例。 Then you can use that variable to execute interface methods of that instance without caring about the specific implementation being used.
然后,您可以使用该变量执行该实例的接口方法,而不必关心所使用的特定实现。 It makes your code much more general, since you can switch to a different implementation of the interface without changing the code that uses the variable of the interface type.
它使您的代码更加通用,因为您可以切换到接口的其他实现,而无需更改使用接口类型的变量的代码。
You are making an "Anonymous Class" in the second code block. 您正在第二个代码块中创建一个“匿名类”。 This means that it creates a class that implements the interface or class that you write.
这意味着它将创建一个实现您编写的接口或类的类。 Its basically a short hand for making a subclass that implements the interface (MyInterface)
从本质上讲,它是制作实现接口(MyInterface)的子类的快捷方式
I want to know whats going on under the hood.
我想知道引擎盖下发生了什么。
Consider the code below, assuming the interface MyInterface
defined in your question. 考虑下面的代码,假设您的问题中定义了接口
MyInterface
。
Two inner classes are defined; 定义了两个内部类; the first class is anonymous (having no name) and the second class is named
MyClass
. 第一类是匿名的(没有名称),第二类是
MyClass
。 Each of these two classes implements
MyInterface. 这两个类均
implements
MyInterface。
// declare variable of type MyInterface
MyInterface myVariable;
// assign the variable to an instance of anonymous class that implements MyInterface
myVariable = new MyInterface() {
@Override
public void do_it_now() {
}
};
// define a named class that implements MyInterface
class MyClass implements MyInterface {
@Override
public void do_it_now() {
}
}
// assign the variable to an instance of named class that implements MyInterface
myVariable = new MyClass();
What's going on under the hood is, the java compiler compiles new MyInterface() {...};
实际情况是,java编译器会编译
new MyInterface() {...};
into a separate class file with a name like $1.class
, just like it compiles MyClass
into a separate class file with a name like MyClass$1.class
. 进入一个名为
$1.class
的单独的类文件,就像将MyClass
编译为一个名为MyClass$1.class
的单独的类文件一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.