public class StaticInnerClass { static int a=12; //静态内部类 public static class Moo{ int b=13; public void add(){ int c=14; System.out.println(a); } } public static void main(String[] args) { Moo m=new Moo(); m.add(); } }
1 2 3 4 5 6 7 8 9 10
import com.qf.oop.innerclass.StaticInnerClass; import com.qf.oop.innerclass.StaticInnerClass.Moo; public class Test { public static void main(String[] args) { /*StaticInnerClass.Moo m=new StaticInnerClass.Moo(); m.add();*/ Moo m=new Moo(); m.add(); } }
public class LocInnerClass { public static void main(String[] args) { final int a=12; class Moo{ int b=13; public void add(){ int c=14; // a++; System.out.println(a); } } Moo m=new Moo(); m.add(); LocInnerClass l=new LocInnerClass(); l.test(); } public void test(){ final int a=12; class Moo{ int b=13; public void add(){ int c=14; // a++; System.out.println(a); System.out.println(this); } } Moo m=new Moo(); m.add(); } }
public class InnerClass2 { int a=12; //成员内部类 public class Moo{ int a=13; public void add(){ int a=14; System.out.println(a); System.out.println("内部类成员变量a="+new Moo().a); System.out.println("内部类成员变量a="+this.a); System.out.println("外部类的成员变量a="+new InnerClass2().a); System.out.println("外部类的成员变量a="+InnerClass2.this.a); } } public static void main(String[] args) { //第二种方式 InnerClass2.Moo m3=new InnerClass2().new Moo(); m3.add(); } }