關於內部類

對於爲什麼要使用內部類,網上很多都是扯淡,這個我也不想多說,直接貼權威:

Why Use Nested Classes?

There are several compelling reasons for using nested classes, among them:

  • It is a way of logically grouping classes that are only used in one place.
  • It increases encapsulation.
  • Nested classes can lead to more readable and maintainable code.

Logical grouping of classes—If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.

Increased encapsulation—Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.

More readable, maintainable code—Nesting small classes within top-level classes places the code closer to where it is used.

 

/*
*	@topic:外部類(非主類)通過怎樣的方式訪問內部類
*/
class Outer{
	class Inner{
		private String name ;
		Inner(){};
		Inner(String n){
			this.name = n; 
		}
		public void setName(String n){
			this.name = n;
		}
		public String getName(){
			return this.name;
		}	
	}

	public Inner getInnerClass(){
		return new Inner("Landor");
	}
}
public class InnerClassDemo{
	public static void main(String[] args) {
		//System.out.println("Hello Landor!");
		Outer out = new Outer();
		System.out.println(out.getInnerClass().getName());
	}
}

/*
*	@topic:內部類能不能訪問宿主類的私有屬性
*/
class Outer{
	private String str = "Sugite" ;
	class Inner{
		private String name ;
		Inner(){
			this.name = str;
		};
		Inner(String n){
			this.name = n; 
		}

		public void setName(String n){
			this.name = n;
		}

		public String getName(){
			return this.name;
		}	
	}

	public Inner getInnerClass(){
		return new Inner();
	}
}
public class InnerClassDemo{
	public static void main(String[] args) {
		//System.out.println("Hello Landor!");
		Outer out = new Outer();
		System.out.println(out.getInnerClass().getName());
	}
}

/*
*	@topic:內部類能不能繼承別的類和接口
*/
class A{
	public void prt(){
		System.out.println("Landor");
	}
}
class Outer{
	class Inner extends A{
	}

	public Inner getInnerClass(){
		return new Inner();
	}
}
public class InnerClassDemo{
	public static void main(String[] args) {
		//System.out.println("Hello Landor!");
		Outer out = new Outer();
		out.getInnerClass().prt();
	}
}

/*
*	@topic:內部類能不能是static的
*/
class Outer{
	static class Inner{
		public void prt(){
			System.out.println("Landor");
		}
	}
}
public class InnerClassDemo{
	public static void main(String[] args) {
		//System.out.println("Hello Landor!");
		new Outer.Inner().prt(); //可以直接通過外部內.內部類來訪問
	}
}
/*
*	@topic:外部訪問內部類
*/
class Outer{
	class Inner{
		public void prt(){
			System.out.println("Landor");
		}
	}
}
public class InnerClassDemo{
	public static void main(String[] args) {
		//System.out.println("Hello Landor!");
		Outer out = new Outer();
		Outer.Inner in = out.new Inner();
		in.prt();
	}
}
/*
*	@topic:匿名內部類的實現
*/
abstract class B{
	abstract void fun();
}
class Outer{
	public B out(){
		return new B(){//匿名內部類
			void fun(){
				System.out.println("Landor");
			}
		};
	}
}
public class InnerClassDemo{
	public static void main(String[] args) {
		//System.out.println("Hello Landor!");
		new Outer().out().fun();
	}
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章