使用继承和多态性来实例化Thread子类对象之间的主要区别是什么?

huangapple go评论71阅读模式
英文:

What is the main difference between using inheritance and polymorphism to instantiate of an object of a subclass of Thread?

问题

以下是翻译好的内容:

这是 Thread 的一个子类:

public class AnotherThread extends Thread{

    @Override
    public void run() {
        System.out.println("Hello from another thread.");
    }
}

在这段代码中,为什么我们要使用多态性来实例化 AnotherThread 类的对象,其中 AnotherThread 是 Thread 的一个子类:

public static void main(String[] args) {

    System.out.println("This is the main thread.");

    Thread anotherThread = new AnotherThread();
    anotherThread.start();
}

而在这段代码中,为什么我们要使用继承来实例化 AnotherThread 类的对象,其中 AnotherThread 是 Thread 的一个子类:

public static void main(String[] args) {

    System.out.println("This is the main thread.");

    AnotherThread anotherThread_2 = new AnotherThread();
    anotherThread_2.start();
}
英文:

What is the main difference between these two following codes in multiple threading when we have a class such as AnotherThread and it extends Thread and we override run method inside it.
When do we need to use polymorphism for creating an instance of AnotherThread class?
And when we have to use inheritance for creating an of AnotherThread class?

This is a subclass of Thread:

public class AnotherThread extends Thread{

    @Override
    public void run() {
        System.out.println("Hello from another thread.");
    }
}

in this code why we use Polymorphism to instantiate of an object of AnotherThread class where AnotherThread is a subclass of Thread:

public static void main(String[] args) {

    System.out.println("This is the main thread.");


    Thread anotherThread = new AnotherThread();
    anotherThread.start();

And in this code why we use inheritance to instantiate of an object of AnotherThread class where AnotherThread is a subclass of Thread:

public static void main(String[] args) {

    System.out.println("This is the main thread.");

    AnotherThread anotherThread_2 = new AnotherThread();
    anotherThread_2.start();

答案1

得分: 2

完全在线程的实例化中没有任何区别。唯一的区别在于持有对线程引用的变量的类型。

第一个问题:你理解“对象”和“对对象的引用”之间的区别吗?一个变量不是对象,它持有一个引用。

所以问题是,你希望你的程序将线程视为通用的Thread(第一个例子),还是视为AnotherThread(第二个例子)?这两者都是有效的,这取决于你想要实现什么。一般来说,如果后续的代码不需要将其视为AnotherThread,那么编程到Thread接口更为推荐。

英文:

There is no difference AT ALL in the instantiation of the thread. The only difference is in the type of the variable that holds the reference to the thread.

First question: do you understand the difference between an object and a reference to an object? A variables is not the object, it holds a reference.

So the question is, do you want your program to treat the thread as a general Thread (first example) or as AnotherThread (second example)? Both of these are valid, it just depends on what you want to accomplish. In general, if subsequent code has no need to treat it specifically as AnotherThread, then programming to the Thread interface is preferred.

huangapple
  • 本文由 发表于 2020年10月5日 06:53:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64200628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定