第十章 内部类

public class TestInnerClass {
	public class Inner {
	}

	public static void main(String[] args) {
		TestInnerClass t = new TestInnerClass();
		TestInnerClass.Inner i = t.new Inner();//少见的语法
	}
}

内部类,可以访问其外围内的所有属性和方法。在拥有外部类对象之前是不可能创建内部类对象的,这是因为内部类对象会暗暗地连接到创建它的外部类对象上,但是如果你创建的是嵌套类(静态内部类),那么他就不需要对外部类对象的引用。

内部类某个接口的实现方式能够完全不可见,并且不可用,能方便的隐藏细节如下:

interface Destiantion{
	String readLable();
}
class Parcel4{
	private class PContents implements Destiantion{
		public PContents(String str) {
			System.out.println(str);
		}
		@Override
		public String readLable() {
			// TODO Auto-generated method stub
			return "aaaaa";
		}
	}
	public Destiantion destination(String str){
		return new PContents(str);
	}
}
public class TestInnerClass {
	public static void main(String[] args) {
		Parcel4 p=new Parcel4();
		Destiantion d=p.destination("dddd");
		d.readLable();
//		Parcel4.PContents pc=p.new PContents("dddd");//当PContents为private时候,完全不可用
	}
}
内部类还可以在方法中和作用域中定义,不过在该方法或是作用域外不可用。

当内部类需要直接使用其他的外部类,需要加final修饰,除非内部类初始化的时候其他的外部类当参数传入。

先看下匿名内部吧,没有名字的类,与正规类比起来有些局限性,比如只能实现一个接口;因为用的时候也是向上转型,只能用父类的方法:

interface Destiantion{
	String readLable();
}
class Contents{
	int value(){
		System.out.println("dddd");
		return 0;
	}
}
public class TestInnerClass {
	public static void main(String[] args) {
		TestInnerClass t=new TestInnerClass();
		Destiantion d=t.getDestiantion();
		Contents c=t.getContents();
	}
	public Destiantion getDestiantion(){
		return new Destiantion() {
			
			@Override
			public String readLable() {
				// TODO Auto-generated method stub
				return "dddd";
			}
		};
	}
	public Contents getContents(){
		return new Contents() {
			@Override
			public int value() {
				// TODO Auto-generated method stub
				return 5;
			}
		};
	}
}
了解匿名内部类,我们再看看工厂方法:
interface Service{
	void method1();
	void method2();
}
interface ServiceFactory{
	Service getService();
}
class Implementation1 implements Service{
	private Implementation1(){}

	@Override
	public void method1() {
		System.out.println("Implementation1  method1");
	}

	@Override
	public void method2() {
		System.out.println("Implementation1  method2");
	}
	public static ServiceFactory factory=new ServiceFactory() {
		
		@Override
		public Service getService() {
			// TODO Auto-generated method stub
			return new  Implementation1();
		}
	};
}
class Implementation2 implements Service{
	private Implementation2(){}

	@Override
	public void method1() {
		System.out.println("Implementation2  method1");
	}

	@Override
	public void method2() {
		System.out.println("Implementation2  method2");
	}
	public static ServiceFactory factory=new ServiceFactory() {
		
		@Override
		public Service getService() {
			// TODO Auto-generated method stub
			return new  Implementation2();
		}
	};
}
public class TestInnerClass {
	public static void serviceConsumer(ServiceFactory fact){
		Service s=fact.getService();
		s.method1();
		s.method2();
	}
	public static void main(String[] args) {
		serviceConsumer(Implementation1.factory);
		serviceConsumer(Implementation2.factory);
	}
}
如果不需要内部类对象与外围类对象之间有联系,那么可以将内部类声明为static,这通常称为嵌套类。

内部类最吸引人的原因是:

每个内部类都能独立的继承自一个(接口的)实现,所以无论外围类是否已经继承了某个(接口的)实现,对于内部类都没有影响。

class WithInner{
	class Inner{};
}
public class TestInnerClass extends WithInner.Inner{
	public TestInnerClass(WithInner wi) {
		wi.super();
	}
	public static void main(String[] args) {
		WithInner wi=new WithInner();
		TestInnerClass tic=new TestInnerClass(wi);
	}
	
}
关于内部类的继承,重申一遍在拥有外部类对象之前是不可能创建内部类对象的,这是因为内部类对象会暗暗地连接到创建它的外部类对象上,但是如果你创建的是嵌套类(静态内部类),那么他就不需要对外部类对象的引用。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值