[英]Interfaces and Packages
我正在用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
實現接口時,是否可以訪問包含該接口的包中的類? 我的軟件包中沒有其他文件,並且也沒有進行任何類型的導入,因此對於為什么從一開始就可以訪問它,我確實感到困惑。
從來沒有看到過這樣的警告,所以在這里走了出來...您為課程定義了一個包裝嗎? 否則,這可能意味着Thread t
成員具有大多數書籍所稱的默認可見性或程序包私有的可見性(這意味着程序包和類級別的可見性),因為該字段沒有可見性修改器。 Java具有4種不同的可見性:公共,默認,受保護,私有。 有關更多信息,請參見此處: 控制對類成員的訪問
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.