為什么不能加this
//外部類
public class SOuter {
?private int a=99;//外部類的私有變量
? static int b=11;//外部類靜態(tài)變量
?//靜態(tài)內(nèi)部類
? public static class SIuter{
?int ?b=22;//內(nèi)部類的變量
?public void test(){
?System.out.println("訪問外部類中的b;"+SOuter.this.b);
?System.out.println("訪問內(nèi)部類中的b:"+b);
?}
? }
? //測試靜態(tài)內(nèi)部類
? public static void main(String [] args){
?//直接創(chuàng)建內(nèi)部類對象
?SIuter si=new SIuter();
?//調(diào)用test方法
?si.test();
? }
}
2017-08-12
都是靜態(tài)變量,雖然名字相同但是不需要加this。
2017-08-12
外部類中的b是static,因此不需要Outer.this.b
2017-08-12
您好,我的理解是因為你的b已經(jīng)是外部類的靜態(tài)變量.而this是不能在靜態(tài)方法中出現(xiàn)的;