英文:
Inheritance issues with thread class Java
问题
我有这个线程类:
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();
然后编译器告诉我:"The constructor "tCalcVertex(String)" does not exist"。我要如何在不必重新编写整个类的情况下继承这个类?
英文:
I have this thread class:
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 ();
}
I tried to inherit from this by doing this:
class tCalcVertex extends tCallTime{
@Override
void codeToRun(){
print("test");
}
}
I then try to run it by using the following code:
tCallTime thread = new tCallTime("Thread-1");
thread.start();
tCalcVertex thread2 = new tCalcVertex("Tread-2");
thread2.start();
The compiler then tells me that "The constructor "tCalcVertex(String)" does not exist"
How would i go about inheriting from this class without having to rewrite the whole class
答案1
得分: 0
编译器是正确的,您的类 tCalcVertex
中没有接受字符串参数的构造函数。在其父类中只有一个构造函数。构造函数不会自动继承,您必须为层次结构中的每个类显式定义它们:
class tCalcVertex extends tCallTime {
public tCalcVertex(String name) {
super(name);
}
@Override
void codeToRun() {
print("test");
}
}
PS:Java 命名约定使用 PascalCase 命名类,首字母大写。遵循这个约定可以让其他程序员更容易快速理解您的代码。
英文:
Well, the compiler is correct, there is no constructor in your class tCalcVertex
which takes a string. There is only one in its parent class. Constructors are not automatically inherited, you have to define them explicitly for each class in the hierarchy:
class tCalcVertex extends tCallTime {
public tCalcVertex(String name) {
super(name);
}
@Override
void codeToRun() {
print("test");
}
}
PS Java naming conventions name classes in PascalCase with a uppercase letter as first character. Adhering to this convention makes it a lot easier for fellow programmers to understand your code quickly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论