//非静态成员内部类//要求:使用已知的变量,在控制台输出30,20,10。classOuter{publicint num =10;classInner{publicint num =20;publicvoidshow(){int num =30;System.out.println(num);System.out.println(this.num);//(内部类.)this.成员变量System.out.println(Outer.this.num);//外部类.this.成员变量}}}classInnerClassTest{publicstaticvoidmain(String[] args){//调用方式Outer.Inner oi =newOuter().newInner();
oi.show();}}
//静态成员内部类//要求:使用已知的变量,在控制台输出30,20,10。classOuter{publicstaticint num =10;//静态的变量静态内部类才可以访问staticclassInner{publicint num =20;publicvoidshow(){int num =30;System.out.println(num);System.out.println(Inner.this.num);//(内部类.)this.成员变量System.out.println(Outer.num);//外部类.成员变量}}}classInnerClassTest{publicstaticvoidmain(String[] args){//调用方式Outer.Inner oi =newOuter.Inner();
oi.show();}}
interfaceInter{publicstaticfinalint a =23;}publicclassTest{publicstaticvoidmain(String[] args){newInter(){int a =20;publicvoidshow(){//this 代表匿名内部类System.out.println(a);//20 或this.aSystem.out.println(Inter.a);//23}}.show();}}