[英]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 instance
和interface
之间的关系。 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 class
的object 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.