【发布时间】:2020-12-06 23:36:30
【问题描述】:
我有这个线程类:
class tCallTime implements Runnable {
private Thread t;
private String threadName;
public tCallTime(String name) {
threadName = name;
println("Creating " + threadName );
}
tCallTime() {
}
void codeToRun() {
//Override This
callTime();
}
public void run() {
println("Running " + threadName );
try {
codeToRun();
Thread.sleep(0);
}
catch (InterruptedException e) {
println("Thread " + threadName + " interrupted.");
}
}
public void start () {
if (t == null) {
t = new Thread (this, threadName);
t.setPriority(10);
println("Started " + threadName + " with priority " + t.getPriority());
t.start ();
}
我试图通过这样做来继承它:
class tCalcVertex extends tCallTime{
@Override
void codeToRun(){
print("test");
}
}
然后我尝试使用以下代码运行它:
tCallTime thread = new tCallTime("Thread-1");
thread.start();
tCalcVertex thread2 = new tCalcVertex("Tread-2");
thread2.start();
然后编译器告诉我“构造函数“tCalcVertex(String)”不存在” 我将如何从这个类继承而不必重写整个类
【问题讨论】:
-
你至少需要一个构造函数来传递线程名称。
标签: java inheritance multiple-inheritance