关于内部类

对于为什么要使用内部类,网上很多都是扯淡,这个我也不想多说,直接贴权威:

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();
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值