靜態(tài)內(nèi)部類(lèi)訪問(wèn)外部類(lèi)的非靜態(tài)成員score應(yīng)該怎么編程實(shí)現(xiàn)?
//外部類(lèi)
public class HelloWorld {
? ? private ?int score = 84;
? ??
? ? // 創(chuàng)建靜態(tài)內(nèi)部類(lèi)
public ? static class SInner {
? ? ? ? // 內(nèi)部類(lèi)中的變量score
? ? ? ? int score = 91;
? ? ? ??
public void show() {
System.out.println("訪問(wèn)外部類(lèi)中的score:" + ? ? ? score ? );
System.out.println("訪問(wèn)內(nèi)部類(lèi)中的score:" + score);
}
}
// 測(cè)試靜態(tài)內(nèi)部類(lèi)
public static void main(String[] args) {
// 直接創(chuàng)建內(nèi)部類(lèi)的對(duì)象
? ? ? ? SInner si=new SInner();
? ? ? ??
? ? ? ? // 調(diào)用show方法
si.show();
}
}
2017-10-26
System.out.println("訪問(wèn)外部類(lèi)中的score:" + new HelloWorld?().score);
2017-10-26
System.out.println("訪問(wèn)外部類(lèi)中的score:" +? HelloWorld().score ? );