[{ "resource": "/D:/KY8_1.java", "owner": "_generated_diagnostic_collection_name_#2", "code": "1207959691", "severity": 8, "message": "Constructor call must be the first statement in a constructor", "source": "Java", "startLineNumber": 3, "startColumn": 10, "endLineNumber": 3, "endColumn": 21 }]
时间: 2023-07-08 21:53:09 浏览: 49
这个错误信息是因为在 KY8_1 类的构造函数中,调用了 `super(str)`,但是这个调用不是构造函数的第一条语句,而是在 `System.out.println("constructor of KY8_1");` 之后。
为了解决这个问题,你需要将 `super(str)` 移动到构造函数的第一条语句。修改后的代码如下:
```java
public class KY8_1 extends Thread {
public KY8_1(String str) {
super(str);
System.out.println("constructor of KY8_1");
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " " + getName());
try {
sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {}
}
System.out.println("DONE! " + getName());
}
}
public class TwoThreadsTest {
public static void main (String[] args) {
new KY8_1("Go to Beijing??").start();
new KY8_1("Stay here!!").start();
}
}
```
现在,你可以重新编译并运行这个程序,这个错误应该就会消失了。