更新时间:2022-05-01 00:03:48
为什么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
的类。那么你的问题就是为什么这个扩展$ c>的类来自
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 extend
s 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