1

私は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

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

4

1 に答える 1

1

その警告を見たことがないので、ここに足を踏み入れてください...クラス用に定義されたパッケージはありますか?それ以外の場合は、フィールドに可視性修飾子がないため、Thread t-memberがデフォルトの可視性またはパッケージプライベートの可視性(パッケージレベルおよびクラスレベルの可視性を意味する)と呼ばれるものを持っていることを意味する可能性があります。Javaには、パブリック、デフォルト、保護、プライベートの4つの異なる可視性があります。詳細については、こちらを参照してください:クラスのメンバーへのアクセスの制御

于 2012-08-31T05:09:05.077 に答える