我正在用 Java 实现一个接口,并且想知道为什么这段代码:
package threaddemo;
// Create a new thread...
class NewThread implements Runnable {
Thread t;
NewThread(){
// Create a second, new thread...
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start();
}
// This is the entry point for the second thread...
public void run(){
try {
for (int i=0; i<5; i++){
System.out.println("Child thread: " + i);
// Let the thread sleep for a while...
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted...");
}
System.out.println("Exiting child thread...");
} }
public class ThreadDemo {
public static void main(String[] args) {
// Create a new thread...
new NewThread();
try {
for (int i=0; i<5; i++){
System.out.println("Main thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e){
System.out.println("Main thread itnerrupted...");
}
System.out.println("Main thread exiting...");
} }
在其左侧生成以下警告:
Package Field
当您实现接口时,您是否可以访问包含该接口的包中的类?我的包中还没有其他文件,我也没有进行任何类型的导入,所以我实际上有点困惑为什么可以访问它...