私は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
インターフェイスを実装するとき、インターフェイスを含むパッケージ内のクラスにアクセスできますか? パッケージにはまだ他のファイルがなく、インポートも行っていないため、そもそもなぜこれにアクセスできるのかについて、実際にはちょっと混乱しています...