简体   繁体   English

为什么可以将匿名类的实例分配给Interface变量?

[英]Why an instance of anonymous class can be assigned to an Interface variable?

I am new to Java. 我是Java新手。 I have problem to understand the relation between anonymous class instance and interface . 我有理解anonymous class instanceinterface之间的关系。 Refer to the example in this website : 请参阅本网站中的示例:

// anonymous instance as a variable
Runnable r = new Runnable()
{
    @Override
    public void run()
    {
        //codes
    }
};

Thread t1 = new Thread(r, "anonymous 1");

// anonymous instance as a parameter
Thread t2 = new Thread (new Runnable()
{
    @Override
    public void run()
    {
        //codes
    }
}, "anonymous 2");

Based on the answers in this SO question (http://stackoverflow.com/questions/11069056/why-java-interface-can-be-instantiated-in-these-codes), I have a basic understanding of anonymous class. 根据这个SO问题(http://stackoverflow.com/questions/11069056/why-java-interface-can-be-instantiated-in-these-codes)中的答案,我对匿名类有了基本的了解。

However, I don't understand what is this: 但是,我不明白这是什么:

Runnable r = new Runnable(){...};

On the right hand side, we created an instance of an anonymous class, so it is an object of a class . 在右侧,我们创建了一个匿名类的实例,因此它是object of a classobject of a class On the left hand side, it is an Interface variable. 在左侧,它是一个Interface变量。

Why an instance of anonymous class can be assigned to an Interface variable? 为什么可以将匿名类的实例分配给Interface变量?

Refer to this website : 参考这个网站

Thread(Runnable target, String name)

It seems that Thread is expecting the 1st argument to be an Interface variable. 似乎Thread期望第一个参数是一个Interface变量。

This code... 这段代码......

Runnable r = new Runnable(){...};

The class Runnable is an interface, so you are correct that you can't create an instance of it directly. Runnable类是一个接口,因此您无法直接创建它的实例。 However, note the code in the {...} section - this is implementing the methods of the interface in a localised way. 但是,请注意{...}部分中的代码 - 这是以本地化方式实现接口的方法。

Basically what is happening is you are creating an instance of the interface, which is only viewable and usable by the method where r is defined. 基本上正在发生的是你正在创建一个接口实例,它只能由定义r的方法查看和使用。 As you have implemented all the methods in the {...} section, the class is valid and you can use it as you do any other Runnable object. 由于您已在{...}部分中实现了所有方法,因此该类是有效的,您可以像使用任何其他Runnable对象一样使用它。

This code... 这段代码......

Runnable r = new Runnable()
{
    @Override
    public void run()
    {
        //codes
    }
};

Is effectively the same as doing the following... 实际上与执行以下操作有效...

public class MyRunnable implements Runnable {
    public void run()
    {
        //codes
    }
};

// This goes in your class where you want to create the Runnable object
Runnable r = new MyRunnable();

Both pieces of code will create a variable r that is a Runnable instance. 这两段代码都将创建一个变量r ,它是一个Runnable实例。 The first solution is a shorthand for creating the instance, and is useful for something that won't need to be reused anywhere else. 第一个解决方案是创建实例的简写,对于不需要在其他任何地方重用的东西很有用。 The second solution creates a separate class for implementing the interface, and it can be reused outside of the local scope. 第二个解决方案创建了一个单独的类来实现接口,它可以在本地作用域之外重用。

We are able to store the variables with an interface type, provided that the object assigned to the variable implements the interface. 我们能够使用接口类型存储变量,前提是分配给变量的对象实现接口。 So, in the code... 所以,在代码中......

Runnable r = new Runnable(){...};

We are saying that r implements the Runnable interface. 我们说r实现了Runnable接口。 The bit in the {...} is where the interface is implemented, as explained earlier, so it all works out fine. {...}的位是接口的实现位置,如前所述,所以一切正常。

You can assign an object to an interface only if the object class implements that interface. 仅当对象类实现该接口时,才能将对象分配给接口。 In this case, you're creating an object of your anonymous class that implements the Runnable interface, making it a correct assignment. 在这种情况下,您将创建一个实现Runnable接口的匿名类的对象,使其成为正确的赋值。

Also, the Thread class has a non-arg constructor . 此外, Thread类具有非arg构造函数 That's why the code compiles. 这就是代码编译的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Java 中,为什么未将类的 New 实例分配给变量 - In Java why is the New instance of a class not assigned to a variable 为什么匿名内部类可以访问和更新外部类的非final实例变量? - Why the non-final instance variable of the outer class can be accessed and updated in the anonymous inner class? 可以引用匿名类的实例吗? - Can refer to an instance of anonymous class? 当类将实现这些接口时,可以在接口引用变量中分配任何对象 - Any object can be assigned in interface reference variable when class will implement these interface 在匿名内部类中引用实例变量 - Referring instance variable in anonymous inner class 如果一个类实现了一个接口,并且被分配给一个具有该接口类型的变量,那么该对象是什么类型的? - If an class implements an interface, and is assigned to a variable with a type of that interface, what type is that object? 为什么我们不能创建Collections类的实例(不是Collection Interface)? - Why can't we create instance of Collections class (not Collection Interface)? 为什么可以将变量分配给接口类型? - Why can variables be assigned to interface type? 为什么我们不能在没有匿名类方法的情况下在java中实例化接口或抽象类? - Why can't we instantiate an interface or an abstract class in java without an anonymous class method? 为什么实例方法的Java方法引用无法分配给Consumer接口 - Why Java Method Reference of instance method cannot be assigned to Consumer interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM