第十章 内部类

来源:互联网 发布:linux上传文件命令 编辑:程序博客网 时间:2024/05/20 06:05
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);}@Overridepublic String readLable() {// TODO Auto-generated method stubreturn "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() {@Overridepublic String readLable() {// TODO Auto-generated method stubreturn "dddd";}};}public Contents getContents(){return new Contents() {@Overridepublic int value() {// TODO Auto-generated method stubreturn 5;}};}}
了解匿名内部类,我们再看看工厂方法:
interface Service{void method1();void method2();}interface ServiceFactory{Service getService();}class Implementation1 implements Service{private Implementation1(){}@Overridepublic void method1() {System.out.println("Implementation1  method1");}@Overridepublic void method2() {System.out.println("Implementation1  method2");}public static ServiceFactory factory=new ServiceFactory() {@Overridepublic Service getService() {// TODO Auto-generated method stubreturn new  Implementation1();}};}class Implementation2 implements Service{private Implementation2(){}@Overridepublic void method1() {System.out.println("Implementation2  method1");}@Overridepublic void method2() {System.out.println("Implementation2  method2");}public static ServiceFactory factory=new ServiceFactory() {@Overridepublic Service getService() {// TODO Auto-generated method stubreturn 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);}}
关于内部类的继承,重申一遍在拥有外部类对象之前是不可能创建内部类对象的,这是因为内部类对象会暗暗地连接到创建它的外部类对象上,但是如果你创建的是嵌套类(静态内部类),那么他就不需要对外部类对象的引用。



0 0
原创粉丝点击