1. 程式人生 > >Java內部類的定義及在外部使用的方法

Java內部類的定義及在外部使用的方法

1、靜態內部類

public class Outer {
	
	static class StaticInner{
		public void show() {
			System.out.println("StaticInner");
		}
	}

	public static void main(String[] args) {
		Outer.StaticInner inner = new Outer.StaticInner();
		inner.show();
	}

}
2、例項內部類(非靜態內部類)
public class Outer {
	
	class MemberInner{
		public void show() {
			System.out.println("MemberInner");
		}
	}

	public static void main(String[] args) {
		Outer outer = new Outer();
		Outer.MemberInner inner = outer.new MemberInner();
		inner.show();
	}

}

3、靜態內部類訪問巢狀它的外部類的靜態成員
public class Outer {
	
	private static int sa = 5;
	private int ia = 10;
	
	static class StaticInner{
		public void show() {
			//靜態內部類可以訪問巢狀它的外部類的靜態成員
			System.out.println("Outer's sa = " + sa);
			//Error。靜態內部類不能訪問巢狀它的外部類的例項成員
			//System.out.println("Outer's ia = " + ia);
		}
	}

	public static void main(String[] args) {
		Outer.StaticInner inner = new Outer.StaticInner();
		inner.show();
	}

}


4、例項內部類訪問巢狀它的外部類的成員

public class Outer {
	
	private static int sa = 5;
	private int ia = 10;
	
	class MemberInner{
		public void show() {
			System.out.println("Outer's sa = " + sa);
			System.out.println("Outer's ia = " + ia);
		}
	}

	public static void main(String[] args) {
		Outer outer = new Outer();
		Outer.MemberInner inner = outer.new MemberInner();
		inner.show();
	}

}

5、內部類跟巢狀它的外部類屬性重名時的訪問方法
public class Outer {
	
	private static int sa = 1;
	private int ia = 2;
	
	static class StaticInner{
		
		private static int sa = 10;
		private int ia = 20;
		
		public void show() {
			System.out.println("From StaticInner: Outer's sa = " + Outer.sa);
			System.out.println("From StaticInner: StaticInner's ia = " + ia);
		}
	}
	
	class MemberInner{
		//Error。例項內部類中不能有非常量的靜態屬性
		//private static int ta = 1000;
		private static final int ta = 100;
		private int ia = 200;
		
		//Error。例項內部類中也不能有靜態的方法
		/*public static void nothing() {
		}*/
		
		public void show() {
			System.out.println("From MemberInner: Outer's sa = " + Outer.sa);
			System.out.println("From MemberInner: Outer's ia = " + Outer.this.ia);
			
			System.out.println("From MemberInner: MemberInner's sa = " + MemberInner.ta);
			System.out.println("From MemberInner: MemberInner's ia = " + this.ia);
		}
	}

	public static void main(String[] args) {
		Outer.StaticInner si = new Outer.StaticInner();
		si.show();
		
		Outer.MemberInner mi = new Outer().new MemberInner();
		mi.show();
	}

}

6、方法中定義的內部類

public class Outer {
	
	private static String staticOuterMsg = "static outer message";
	private String memberOuterMsg = "member outer message";
	
	public void show(String argMsg) {
		String normalLocalMsg = "normal local message";
		final String finalLocalMsg = "final local message";
		class Inner{
			public void print() {
				System.out.println(staticOuterMsg);
				System.out.println(memberOuterMsg);
				System.out.println(finalLocalMsg);
				//Error。方法中的內部類只能訪問final區域性變數
				//System.out.println(normalLocalMsg);
				//Error。方法中的內部類只能訪問final區域性變數。
				//System.out.println(argMsg);
			}
		}
		
		new Inner().print();
	}

	public static void main(String[] args) {
		new Outer().show("argument message");
	}

}