创建内部类对象 :
Outer.Inner oi = new Outer().new Inner(); oi.method();
class Outer{ private int num = 10; private class Inner{ int a = 1; public void method() { System.out.println(num); } } public void print() { Inner i= new Inner(); i.method(); } }
成员内部类 私有使用
本类中的方法内,创建内部类对象使用
静态内部类 :
Out.In o1 = new Out.In();
public class demon3 { public static void main(String[] args) { Out.In o1 = new Out.In(); o1.methon(); Out.In.print(); } } class Out{ static class In{ public void methon() { System.out.println("Out.In.methon()"); } public static void print() { System.out.println("Out.In.print()"); } } }
题目:
public class demon4 { public static void main(String[] args) { wai.nei wn = new wai().new nei(); wn.show(); } } class wai{ int num = 10; public class nei{ int num = 20; public void show() { int num = 30; System.out.println(num); // 30 谁最近用谁 System.out.println(this.num); // 20 System.out.println(wai.this.num); // 10 } } }