为什么Thread不是抽象类而start()不是final? [英] Why is Thread not an abstract class and start() not final?

查看:292
本文介绍了为什么Thread不是抽象类而start()不是final?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 Thread 类是作为常规类而不是抽象类实现的,而 run()方法是抽象的。

Why was the Thread class implemented as a regular class and not an abstract class with run() method being abstract.

是否可能引入任何问题?或者以这种方式有用吗?

Will it possibly introduce any problems? Or does it have any use in being this way?

此外,假设 Thread.start()方法是一个非常具体的方法其功能不能由任何其他类实现(如果我没有错)。因此,我认为 final 关键字比其他任何方法更适合。

Also, the Thread.start() method is supposed to be a very specific method whose functionality cannot be implemented by any other class (If I am not wrong). And hence I guess the final keyword would be apt for this more than any other method.

但我能够重写此方法并按我喜欢的方式使用它,

But I am able to override this method and use it as I like,

public class Test extends Thread {
    public static void main (String... args) {
        Thread test = new Test();
        test.start();
    }

    @Override
    public void run() {
        System.out.println("New thread started...");
    }

    @Override
    public void start() {
        System.out.println("Did anyone tell you I will spawn a new thread??");
    }
}

显然只打印,


有没有人告诉你我会产生一个新线程?

Did anyone tell you I will spawn a new thread??

除了混淆工程师替换你之外,是否有任何使用?

Is there any use in overriding other than confusing the engineer replacing you?

如果没有,为什么方法在Thread类中没有被声明为final?

If not, why was the method not declared final in Thread class?

推荐答案


为什么Thread类实现为常规类而不是带有run()方法的
抽象类是抽象的。

Why was the Thread class implemented as a regular class and not an abstract class with run() method being abstract.

这个问题实际上归结为这样一个事实:你应该总是更喜欢构图而不是继承。

This question actually boils down to the fact that you should always prefer composition over inheritance.

如果 Thread 类被声明为 abstract ,则该语言必须提供另一个从程序员可以用来创建 Thread 的类。那么你的问题就是为什么这个扩展的类来自 Thread 不是 abstract 。如果该语言没有从 Thread 提供 extends 的另一个类,程序员必须创建自己的类。 c $ c>从 Thread 扩展并覆盖 run()方法。

If the Thread class was declared as abstract, the language would have to provide another class that extended from it which programmers could use to create a Thread. Your question would then be about why this class that extends from Thread is not abstract. If the language did not provide another class that extends from Thread, programmers would have to create their own class that extends from Thread and override the run() method.


如果没有,为什么方法在Thread类中没有被声明为final?

If not, why was the method not declared final in Thread class??

我能给出的唯一可能的解释是,当该类被引入时,该语言的开发人员会看到一些用例来覆盖 start JDK。我使用的第一个Java版本是1.5,我个人没有遇到过用例,我发现需要覆盖 start 。正如JB Nizet在他的回答中所述

The only possible explanation I can give is that the developers of the language saw some use-cases for overriding start when the class was introduced to the JDK. The first version of Java that I used was 1.5 and I personally have not come across a use-case where I found the need to override start. As JB Nizet stated in his answer


如果今天从头开始重新设计Java,那么设计很可能会有所不同

if Java was redesigned from scratch today, there is a good chance the design would be different

这篇关于为什么Thread不是抽象类而start()不是final?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆