2012-08-31 20 views
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 

當你實現接口,你是否可以訪問包含該接口的包中的類?我的包裏還沒有其他文件,我也沒有做任何類型的導入,所以我實際上有點困惑,因爲爲什麼這是可以開始...

+0

你可以請發佈完整的警告?你的最後一段現在很難理解。 –

+0

除此之外,它並沒有真正說出任何東西。如果有幫助,我在下面發佈了更多信息。 :-) –

+0

如果有幫助,我添加了其餘的代碼。 –

回答

1

從未見過警告,所以在這裏出去......你有沒有爲班級定義的包?否則,它可能意味着Thread t -member有什麼最書叫默認知名度或包私人可見性(這意味着封裝和類級別的可見性),因爲有該字段不可見性修飾符。 Java有4種不同的可見性:public,default,protected,private。在這裏看到更多的信息:Controlling Access to Members of a Class

+0

我在想,但我有點困惑,因爲我沒有導入任何包(而且我的包不包含任何其他類),我沒有創建這個類。任何想法? –

+0

你在使用什麼編輯器,我不記得在Eclipse中看到這個警告......無論如何,這是否意味着你缺少'NewThread類實現Runnable'前面的'public'-modifier,所以這個類只是對同一個包可見? – esaj

+0

我正在使用NetBeans。 :-) –