我这个编译就会报以下错误:
No enclosing instance of type testinterrupt is accessible. Must qualify the allocation with an enclosing instance of type testinterrupt (e.g. x.new A() where x is an instance of testinterrupt).
不知道是怎么回事?百度一下,好像是内部类的原因,但别人给的答案好像又看不懂,希望csdn的高手指点下,先谢谢了!import java.util.*;public class testinterrupt {
/* sleep()是静态方法,通过类名调用。里面的参数表示多少毫秒 */
public static void main(String[] args) {
ThreadSleepDemo ts = new ThreadSleepDemo(); // 通过继承Thread来创建一个线程
ts.start(); // 启动线程
try {
Thread.sleep(10000);
// System.out.println("###############");
} catch (InterruptedException e) {
ts.interrupt();
}
} public class ThreadSleepDemo extends Thread {
public void run() {
// super.run();
while (true)
try {
System.out.println(new Date());
Thread.sleep(1000);
} catch (InterruptedException e)
return;
}
}
}
}
No enclosing instance of type testinterrupt is accessible. Must qualify the allocation with an enclosing instance of type testinterrupt (e.g. x.new A() where x is an instance of testinterrupt).
不知道是怎么回事?百度一下,好像是内部类的原因,但别人给的答案好像又看不懂,希望csdn的高手指点下,先谢谢了!import java.util.*;public class testinterrupt {
/* sleep()是静态方法,通过类名调用。里面的参数表示多少毫秒 */
public static void main(String[] args) {
ThreadSleepDemo ts = new ThreadSleepDemo(); // 通过继承Thread来创建一个线程
ts.start(); // 启动线程
try {
Thread.sleep(10000);
// System.out.println("###############");
} catch (InterruptedException e) {
ts.interrupt();
}
} public class ThreadSleepDemo extends Thread {
public void run() {
// super.run();
while (true)
try {
System.out.println(new Date());
Thread.sleep(1000);
} catch (InterruptedException e)
return;
}
}
}
}
ThreadSleepDemo ts = new ThreadSleepDemo(); // 通过继承Thread来创建一个线程
中的 new ThreadSleepDemo(); 下面出现一条红色的波浪线,最后编辑就会出现上面的错误提示
import java.util.*;public class testinterrupt {
/* sleep()是静态方法,通过类名调用。里面的参数表示多少毫秒 */
public static void main(String[] args) {
ThreadSleepDemo ts = (new testinterrupt()).new ThreadSleepDemo(); // 通过继承Thread来创建一个线程 ts.start(); // 启动线程
try {
Thread.sleep(10000);
// System.out.println("###############");
} catch (InterruptedException e) {
ts.interrupt();
}
} public class ThreadSleepDemo extends Thread {
public void run() {
// super.run();
while (true)
try {
System.out.println(new Date());
Thread.sleep(1000);
} catch (InterruptedException e)
return;
}
}
}
}
最终我改成了下面的代码,但是什么结果都没显示import java.util.*; public class Testinterrupt {
public static void main(String[] args) {
//先实例化外部类,然后实例化内部类
ThreadSleepDemo ts = (new Testinterrupt()).new ThreadSleepDemo(); // 通过继承Thread来创建一个线程 ts.start(); // 启动线程
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
ts.interrupt();
}
} public class ThreadSleepDemo extends Thread {
public void run() {
// super.run();
while (true)
try {
System.out.println(new Date());
Thread.sleep(1000);
} catch (InterruptedException e){
return;
}
}
}
}