新手关于变量输出的问题
class wai{
private int x=10;
public void show(){
final int x=20;
class nei{
int x=30;
public void method(){
System.out.println("x="+x);//这个地方如何打印出变量x的等于20的值呢(如打印x为10的值:wai.this.x,如果打印x等于30的值:x,那如果打印x等于20的值呢,该如何写呢)
}
}
nei n=new nei();
n.method();
}
}
class neiwai{
public static void main(String args[]){
wai y=new wai();
y.show();
}
}
------解决方案--------------------class Wai1 { private int x=10; public void show() { final int x=20; //内部类Nei1,不覆盖方法里的变量x. class Nei1 { int y=30; public void method1() { System.out.println("x in class Wai =" +Wai1.this.x+"\r\nx in method show()= "+x); System.out.println("y in method ="+y); } } //内部类2,把内部类里的变量挪到方法里面,先输出后覆盖。 class Nei2 { public void method2() { System.out.println("x in class Wai =" +Wai1.this.x+"\r\nx in method show()= "+x); int x=30; System.out.println("x in method ="+x); } } Nei1 n1=new Nei1(); n1.method1(); Nei2 n2=new Nei2(); n2.method2(); } } class Neiwai1 { public static void main(String args[]) { Wai1 y=new Wai1(); y.show(); } }