Java內部類入門示例

鍾超發表於2011-11-06
public class test {
	public static void main(String[] args) {
		OuterClass out = new OuterClass();
		OuterClass.InnerClass1 inner1 = new OuterClass().new InnerClass1();
		OuterClass.InnerClass2 inner2 = out.new InnerClass2();
		out.method();
		inner1.method();
		inner2.method();
	}
}

class OuterClass {
	int outerData;
	void method() {
		System.out.println("Outer Class.");
	}
	class InnerClass1 {
		int data;
		void method() {
			System.out.println("Inner Class 1.");
		}
	}
	class InnerClass2 {
		int data;
		void method() {
			System.out.println("Inner Class 2.");
		}
	}
}

相關文章