简体   繁体   English

为什么尽管无法实例化接口,我们仍可以创建接口实例变量?

[英]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.

相关问题 为什么我们不能创建Collections类的实例(不是Collection Interface)? - Why can't we create instance of Collections class (not Collection Interface)? 为什么接口和抽象方法无法实例化? - why interface and abstract methods can't be instantiated? 我们可以创建一个接口的对象吗? - Can we create an object of an interface? 为什么我们不能在INTERFACE的静态块内分配变量? OCA - Why we can't assign a variable inside a static block in INTERFACE ? OCA 为什么可以将匿名类的实例分配给Interface变量? - Why an instance of anonymous class can be assigned to an Interface variable? 为什么我不能使用实例变量访问接口的静态方法 - Why can't I access static methods of an interface using an instance variable 为什么我们不能在功能界面中重载抽象方法? (JAVA) - Why can't we overload a abstract method in a functional interface? (Java) 我们可以创建Runnable的对象作为它的接口 - Can we create the object of Runnable being it interface 为什么我们不能实例化一个接口但可以做一个接口数组? - Why cant we instantiate an interface but can do an array of interface? ClassCastException:无法创建Interface的实例 - ClassCastException: can't make an instance of Interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM