如果我们同时使用它们如何工作没有错误扩展Thread实现Runnable? [重复]

时间:2022-10-28 17:34:00

This question already has an answer here:

这个问题在这里已有答案:

How its works without error if we use both extends Thread implements Runnable together ?

如果我们同时使用它们如何工作没有错误扩展Thread实现Runnable?

public class MyThread extends Thread implements Runnable{

@Override
public void run() {
    // TODO Auto-generated method stub

}
}

Edit: Thread already implements Runnable, so its meaning less to use both.

编辑:线程已经实现了Runnable,所以它的含义更少用于两者。

If I declare a class like this

如果我宣布这样的一个类

MyThread extends implements Runnable, Runnable{
   ...
}

Its "Duplicate interface Runnable for the type MyThread", so we cant compile.

它的“MyThread类型的重复接口可运行”,所以我们无法编译。

Then why we didn't get error for this

那么为什么我们没有得到这个错误

public class MyThread extends Thread implements Runnable{ .. }

2 个解决方案

#1


1  

It won't hurt, but Thread already implements Runnable, so it's repetitive.

它不会受到伤害,但Thread已经实现了Runnable,所以它是重复的。

#2


0  

Yes, We can do it as pointed out above. There is no restriction on that.

是的,我们可以按照上面的指示去做。对此没有限制。

But Honestly, I can not think of a logical scenario for it. As interface implies behavior, and If super class is implementing the public behavior(As all methods of interface are public) of interface, child will inherit the implemented behavior from super class.

但老实说,我无法想到它的逻辑情景。由于接口意味着行为,并且如果超类正在实现接口的公共行为(因为接口的所有方法都是公共的),则child将从超类继承已实现的行为。

#1


1  

It won't hurt, but Thread already implements Runnable, so it's repetitive.

它不会受到伤害,但Thread已经实现了Runnable,所以它是重复的。

#2


0  

Yes, We can do it as pointed out above. There is no restriction on that.

是的,我们可以按照上面的指示去做。对此没有限制。

But Honestly, I can not think of a logical scenario for it. As interface implies behavior, and If super class is implementing the public behavior(As all methods of interface are public) of interface, child will inherit the implemented behavior from super class.

但老实说,我无法想到它的逻辑情景。由于接口意味着行为,并且如果超类正在实现接口的公共行为(因为接口的所有方法都是公共的),则child将从超类继承已实现的行为。