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

发布于 2025-01-28 17:19:03 字数 1128 浏览 2 评论 0 原文

我是Java的新手。我有问题要了解匿名类实例接口之间的关系。请参阅此网站

// 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");

基于此答案的答案)所以问题(http://stackoverflow.com/questions/11069056/why-java-interface-can-be-be-instantiastiatian-in--in-codes),我对匿名类有一个基本的理解。

但是,我不明白这是什么:

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

在右侧,我们创建了一个匿名类的实例,因此它是类的对象。在左侧,它是接口变量。

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

请参阅此 nebsite

Thread(Runnable target, String name)

:该线程期望第一个参数是接口变量。

I am new to Java. I have problem to understand the relation between anonymous class instance and 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.

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. On the left hand side, it is an Interface variable.

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

Refer to this website:

Thread(Runnable target, String name)

It seems that Thread is expecting the 1st argument to be an Interface variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终止放荡 2025-02-04 17:19:03

此代码...

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

运行是一个接口,因此您是正确的,无法直接创建它的实例。但是,请注意 {...} 部分中的代码 - 这是以局部方式实现接口的方法。

基本上,发生的事情是您正在创建接口的实例,只能通过定义 r 的方法可查看和使用。由于您已经在 {...} 部分中实现了所有方法,因此类是有效的,您可以在执行任何其他 Runnable 对象时使用它。

此代码...

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

实际上与执行以下内容相同...

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();

这两个代码都将创建一个变量 r ,即 runnable 实例。第一个解决方案是用于创建实例的速记,对于不需要在其他任何地方重复使用的东西都很有用。第二个解决方案创建了一个单独的类以实现接口,并且可以在本地范围之外重复使用。

我们能够使用接口类型存储变量,只要分配给变量的对象实现了接口即可。因此,在代码中...

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

我们说 r 实现了运行接口。如前所述, {...} 中的位是实现接口的位置,因此一切都很好。

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. 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. 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.

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. 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. The bit in the {...} is where the interface is implemented, as explained earlier, so it all works out fine.

谎言月老 2025-02-04 17:19:03

仅当对象类实现该接口时,您才能将对象分配给接口。在这种情况下,您正在创建一个匿名类的对象,该类别实现运行接口,使其成为正确的分配。

另外, a> class a 。这就是为什么代码编译。

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.

Also, the Thread class has a non-arg constructor. That's why the code compiles.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文