Java编程思想笔记——内部类

内部类:可以将一个类的定义放在另一个类的内部定义。

创建内部类

public class Parcel1 {
    class Contents {
        private int i = 11;
        public int value() { return i; }
    }
    class Destination {
        private String label;
        Destination(String whereTo) {
            label = whereTo;
        }
        String readLabel() { return label; }
    }
    public void ship(String dest) {
        Contents s = new Contents();
        Destination d = new Destination(dest);
        System.out.println(d.readLabel());
    }

    public static void main(String[] args) {
        Parcel1 p = new Parcel1();
        p.ship("Tasmania");
    }
}/*
Tasmania
*/

在这里,实际的区别只是内部类的名字是嵌套在Parce1里面。不过这不是唯一的区别,更典型的情况是,外部类将有一个方法,该方法返回一个指向内部类的引用,就像在to()和contents()方法中看到的一样:

public class Parcel2 {
    class Contents {
        private int i = 11;
        public int value() { return i; }
    }
    class Destination {
        private String label;
        Destination(String whereTo) {
            label = whereTo;
        }
        String readLabel() { return label; }
    }

    public Destination to(String s){
        return new Destination(s);
    }

    public Contents contents(){
        return new Contents();
    }

    public void ship(String dest) {
        Contents s = new Contents();
        Destination d = new Destination(dest);
        System.out.println(d.readLabel());
    }

    public static void main(String[] args) {
        Parcel2 p = new Parcel2();
        p.ship("Tasmania");
        Parcel2 q = new Parcel2();
        Parcel2.Contents c = q.contents();
        Parcel2.Destination d = q.to("Borneo");
    }
}/*
Tasmania
*/

如果想从外部类的非静态方法之外的任意位置创建某个内部类对象,那么必须像在main()方法那样,具体地指明这个对象的类型:OuterClassName.InnerClassName。

链接到外部类

名字隐藏和组织代码的模式。
当生成一个内部类的对象时,此对象与制造他的外围对象(enclosing object)之间就有了一种联系,所以它能访问其外围对象的所有成员,而不需要任何特殊条件。内部类还拥有其外围类的所有元素的访问权。

public class Sequence {
    private Object[] items;
    private int next = 0;
    public Sequence(int size) {
        items = new Object[size];
    }
    public void add(Object x) {
        if(next < items.length) {
            items[next++] = x;
        }
    }
    private class SequenceSelector implements Selector {
        private int i = 0;
        @Override
        public boolean end() {
            return i == items.length;
        }

        @Override
        public Object current() {
            return items[i];
        }

        @Override
        public void next() {
            if(i < items.length){
                i++;
            }
        }
    }
    public Selector selector(){
        return new SequenceSelector();
    }

    public static void main(String[] args) {
        Sequence sequence = new Sequence(10);
        for (int i = 0; i < 10; i++) {
            sequence.add(Integer.toString(i));
        }
        Selector selector = sequence.selector();
        while (!selector.end()) {
            System.out.print(selector.current() + " ");
            selector.next();
        }
    }
}/*
0 1 2 3 4 5 6 7 8 9
*/

SequenceSelector的方法都用到了objects,这是一个引用,并不是SequenceSelector的一部分,而是外围类中的一个private字段。然而内部类可以访问外围类的方法和字段,就像拥有他们似的。
当某个外围类的对象创建了一个内部类对象时,此内部类对象必定会秘密地捕获一个指向那个外围类对象的引用。然后,在访问此外围类的成员时,就是用那个引用来选择外围类的成员。内部类的对象只能在与其外围类的对象相关联的情况下才能被创建(就像应该看到的,在内部类是非static类时)。创建内部类时需要一个指向其外围类对象的引用。

使用.this与.new

如果需要生成对外部类对象的引用,可以使用外部类的名字后面紧跟圆点和this。这样产生的引用自动地具有正确的类型,这一点在编译期就被知晓并受到检查,因此没有任何运行时开销。

public class DotThis {
    void f() {
        System.out.println("DotThis.f()");
    }
    public class Inner {
        public DotThis outer() {
            return DotThis.this;
        }
    }
    public Inner inner() {
        return new Inner();
    }

    public static void main(String[] args) {
        DotThis dt = new DotThis();
        DotThis.Inner dti = dt.inner();
        dti.outer().f();
    }
}/*
DotThis.f()
*/

有时可能要告知某些其他对象,去创建其某个内部类的对象。要实现此目的,你必须在new表达式中提供兑其他外部类对象的引用。

public class DotNew {
    public class Inner {}
    public static  void main(String[] args) {
        DotNew dn = new DotNew();
        DotNew.Inner dni = dn.new Inner();
    }
}

想要直接创建内部类的对象,必须使用外部类的对象创建该内部类对象。
在拥有外部类之前是不可能创建内部类对象的。因为内部类对象会暗暗的连接到创建它的外部类对象上。但是创建嵌套类(静态内部类),就不需要对外部类对象的引用。

public class Parcel3 {
    class Contents {
        private int i = 11;
        public int value() { return i; }
    }
    class Destination {
        private String label;
        Destination(String whereTo) { label = whereTo; }
        String readLabel() { return label; }
    }

    public static void main(String[] args) {
        Parcel3 p = new Parcel3();
        Parcel3.Contents c = p.new Contents();
        Parcel3.Destination d = p.new Destination("Tasmaina");
    }
}

内部类与向上转型

当将内部类向上转型为其基类i,尤其是转型为一个接口的时候,内部类就有了用武之地。是因为此内部类——某个接口的实现——能够完全不可见,并且不可用。所得到的只是指向基类或接口的引用,所以能够很方便地隐藏实现细节。

public interface Destination {
    String readLabel();
}
public interface Contents {
    int value();
}

Contents和Destination表示可用接口。

public class Parcel4 {
    private class PContents implements Contents {
        private int i = 11;

        @Override
        public int value() {
            return i;
        }
    }

    protected class PDestination implements Destination {
        private String label;
        private PDestination(String whereTo) {
            label = whereTo;
        }

        @Override
        public String readLabel() {
            return label;
        }
    }
    public Destination destination(String s) {
        return new PDestination(s);
    }
    public Contents contents() {
        return new PContents();
    }
}
public class TestParcel {
    public static void main(String[] args) {
        Parcel4 p = new Parcel4();
        Contents c = p.contents();
        Destination d = p.destination("Tasmania");
    }
}

Parcel4中增加了一些新的东西:内部类PContents是private,所以除了Parcel4,没有人可以访问,PDestination是protected,所以只有Parcel4及其子类,还有同包的类可以访问。这就意味着,如果客户端程序员想了解或访问这些成员,就会受到限制。实际上,甚至不能向下转型成为private内部类(或protected内部类,除非是继承自它的子类)。于是,private内部类给类的设计者提供了一种途径,通过这种方式可以完全阻止任何依赖于类型的编码,并且完全隐藏了实现的细节。

在方法和作用域内的内部类

可以在一个方法里面或者在任意的作用域内定义内部类,这么做有两个理由:
1.如前所示,你实现了某类型的接口,于是可以创建并返回对其的引用
2.你就要解决一个复杂的问题,想创建一个类来辅助你的解决方案,但又不希望这个类是公共可用的

方法作用域内创建一个完整的类,局部内部类:

public class Parcel5 {
    public Destination destination(String s){
        class PDestination implements Destination{
            private String label;
            private PDestination(String whereTo){
                label = whereTo;
            }
            @Override
            public String readLabel() {
                return label;
            }
        }
        return new PDestination(s);
    }

    public static void main(String[] args) {
        Parcel5 p = new Parcel5();
        Destination d = p.destination("Tasmania");
    }
}

PDestination是destination方法的一部分,而不是Parcel5的一部分。所以在destination之外是不能访问PDestination。return中向上转型——返回的是Destination的引用,他是PDestionation的基类。在destination()中定义了内部类PDestination,并不意味着一旦dest方法执行完毕,PDestiantion就不用了。
可以在同一子目录下的任意类中对某个内部类使用类标识符PDestination,并不会有命名冲突。
在任何作用域内嵌入一个内部类:

public class Parcel6 {
    private void internalTracking(boolean b){
        if(b){
            class TrackingSlip {
                private String id;
                TrackingSlip(String s){
                    id = s;
                }
                String getSlip() { return id; }
            }
            TrackingSlip ts = new TrackingSlip("slip");
            String s = ts.getSlip();
        }
    }
    public void track() {internalTracking(true);}

    public static void main(String[] args) {
        Parcel6 p = new Parcel6();
        p.track();
    }
}

TrackingSlip 类被嵌入到if语句作用域内,这并不是说该类的创建是有条件的,它其实与别的类一起编译过。然而,在定义TrackingSlip 的作用域之外,它是不可用的;除此之外,它与普通类一样。

匿名内部类

public class Parcel7 {
    public Contents contents() {
        return new Contents() {
            private int i = 11;
            @Override
            public int value() {
                return i;
            }
        };
    }

    public static void main(String[] args) {
        Parcel7 p = new Parcel7();
        Contents c = p.contents();
    }
}

contents()方法将返回值的生成与表示这个返回值的类的定义结合在一起。另外,这个类是匿名的,他没有名字。创建一个继承自Contents的匿名类的对象。通过new表达式返回的引用被自动向上转型为对Contents的引用。上诉匿名内部类的语法是下面写法的简化:

public class Parcel7b {
    class MyContents implements Contents {
        private int i = 11;

        @Override
        public int value() {
            return i;
        }
    }
    public Contents contents() { return new MyContents(); }

    public static void main(String[] args) {
        Parcel7b p = new Parcel7b();
        Contents c = p.contents();
    }
}

在匿名内部类中,使用有参数构造器:

public class Wrapping {
    private int i;
    public Wrapping(int x) {i = x;}
    public int value() { return i; }
}
public class Parcel8 {
    public Wrapping wrapping(int x) {
        return new Wrapping(x){
            @Override
            public int value(){
                return super.value() * 47;
            }
        };
    }

    public static void main(String[] args) {
        Parcel8 p = new Parcel8();
        Wrapping w = p.wrapping(10);
    }
}

在匿名类中定义字段时,进行初始化操作:

public class Parcel9 {
    public Destination destination(final String dest){
        return new Destination() {
            private String label = dest;
            @Override
            public String readLabel() {
                return label;
            }
        };
    }

    public static void main(String[] args) {
        Parcel9 p = new Parcel9();
        Destination d = p.destination("Tasmania");
    }
}

定义一个匿名内部类,并且希望他使用一个在外部定义的对象,那么编译器会要求其参数引用是final的,就像你在destination()的参数中看到的那样。如果想做一些类似构造器的行为,在匿名类中不可能使用有名构造器,但通过实例初始化,就能达到匿名内部类创建一个构造器的效果:

public class AnonmousConstructor {
    public static Base getBase(int i){
        return new Base(i) {
            {
                System.out.println("Inside instance initializer");
            }
            @Override
            public void f() {
                System.out.println("In anonymous f()");
            }
        };
    }

    public static void main(String[] args) {
        Base base = getBase(47);
        base.f();
    }
}/*
Base constructor. i = 47
Inside instance initializer
In anonymous f()
*/

不要求变量i一定是final的。因为i被传递给匿名类的基类的构造器,他并不会在匿名类内部被直接使用。

带实例初始化的parcel形式,注意destination的参数必须是final的,因为他们在匿名类内部使用的:

public class Parcel10 {
    public Destination destination(final String dest, final float price) {
        return new Destination() {
            private int cost;
            {
                cost = Math.round(price);
                if(cost > 100){
                    System.out.println("Over budget!");
                }
            }
            private String label = dest;
            @Override
            public String readLabel() {
                return label;
            }
        };
    }

    public static void main(String[] args) {
        Parcel10 p = new Parcel10();
        Destination d = p.destination("Tasmania", 101.395F);
    }
}/*
Over budget!
*/

在实例初始化操作的内部,可以看到有一段代码,他们不能做为字段初始化动作的一部分来执行(if语句)。所以对于匿名类而言,实例初始化的实际效果就是构造器。当然它受到了限制——你不能重载实例初始化方法,所以你仅有一个这样的构造器。
匿名内部类与正规的继承相比有些限制,因为匿名内部类既可以扩展类,也可以实现接口,但不能两种兼备。如果是实现接口也只能实现一个接口。

再访工厂方法
public interface Service {
    void method1();
    void method2();
}
public interface ServiceFactory {
    Service getService();
}
public 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() {
                    return new Implementation1();
                }
            };
}
public 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() {
                    return new Implementation2();
                }
            };
}
public class Factories {
    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);
    }
}/*
Implementation1 method1
Implementation1 method2
Implementation2 method1
Implementation2 method2
*/

现在用于Implementation1和Implementation2的构造器都可以是private的,并且没有任何必要去创建做为工厂的具名类。另外,你经常只需要单一的工厂对象,因此在本例中它没有被创建为Service实现中的一个static域。

public interface Game {
    boolean move();
}
public interface GameFactory {
    Game getGame();
}
public class Checkers implements Game {
    private Checkers(){}
    private int moves = 0;
    private static  final int MOVES = 3;
    @Override
    public boolean move() {
        System.out.println("Checkers move " + moves);
        return ++moves != MOVES;
    }
    public static GameFactory factory = new GameFactory() {
        @Override
        public Game getGame() {
            return new Checkers();
        }
    };
}
public class Chess implements Game{
    private Chess(){}
    private int moves = 0;
    private static  final int MOVES = 4;
    @Override
    public boolean move() {
        System.out.println("Checkers move " + moves);
        return ++moves != MOVES;
    }
    public static GameFactory factory = new GameFactory() {
        @Override
        public Game getGame() {
            return new Chess();
        }
    };
}
public class Games {
    public static void playGame(GameFactory factory){
        Game s = factory.getGame();
        while (s.move()) {
            ;
        }
    }

    public static void main(String[] args) {
        playGame(Checkers.factory);
        playGame(Chess.factory);
    }
}/*
Checkers move 0
Checkers move 1
Checkers move 2
Checkers move 0
Checkers move 1
Checkers move 2
Checkers move 3
*/

在上面一篇最后给出的建议:优先使用类而不是接口,如果你的设计中需要某个接口,你必须了解它。否则,不到迫不得已,不要将其放到你的设计中。

嵌套类

如果不需要内部类对象与其外围对象之间有联系,那么可以将内部类声明为static。这通常称为嵌套类。普通内部类对象隐式地保存了一个引用,指向创建它的外围类对象。然而,当内部类是static的时候,就不一样了。
嵌套类意味着:
1.要创建嵌套类的对象,并不需要其外围类的对象
2.不能从嵌套类的对象中访问非静态的外围类对象
普通内部类的字段与方法,只能放在类的外部层次上,所以普通的内部类不能有static数据和static字段,也不能包含嵌套类。但嵌套类可以包含这些:

public class Parcel11 {
    private static class PacelContents implements Contents{
        private int i = 11;
        @Override
        public int value() {
            return i;
        }
    }
    protected static class ParcelDestination implements Destination{
        private String label;
        private ParcelDestination(String whereTo){
            label = whereTo;
        }
        @Override
        public String readLabel() {
            return label;
        }
        public static void f(){}
        static int x = 10;
        static class AnotherLevel {
            public static void f() {}
            static int x = 10;
        }
    }
    public static Destination destination(String s){
        return new ParcelDestination(s);
    }
    public static Contents contents() {
        return new PacelContents();
    }

    public static void main(String[] args) {
        Contents c = contents();
        Destination d = destination("Tasmania");
    }
}

在main中,没有任何Parcel的Parcel11的对象是必须的;而是使用选取static成员的普通语法来调用——这些方法返回对Contents和Destination的引用。

接口内部的类

嵌套类可以做为接口的一部分。放到接口中的任何类都自动地是public和static的。因为类是static的,只是将嵌套类置于接口的命名空间内,并不违反接口的规则:

public interface ClassInInterface {
    void howdy();
    class Test implements ClassInInterface {
        @Override
        public void howdy() {
            System.out.println("Howdy");
        }

        public static void main(String[] args) {
            new Test().howdy();
        }
    }
}/*
Howdy
*/

如果想创建某些公共代码,使得他们可以被某个接口的所有不同实现所共用,那么使用接口内部的嵌套类会显得很方便。

使用嵌套类来放置测试代码:

public class TestBed {
    public void f(){
        System.out.println("f()");
    }
    public static  class Tester{
        public static void main(String[] args) {
            TestBed t = new TestBed();
            t.f();
        }
    }
}

生成了一个独立的类TestBed TesterrunTestBed T e s t e r , 执 行 r u n T e s t B e d Tester.main()。可以使用这个类来做测试,但是不必在发布的产品中包含他,在将产品打包前可以简单的删除TestBed$Tester.class。

从多层嵌套类中访问外部类的成员

一个内部类被嵌套多少层并不重要——它能透明地访问所有它所嵌入的外围类的所有成员:

public class MNA {
    private void f(){}
    class A {
        private void g() {}
        public class B {
            void h() {
                g();
                f();
            }
        }
    }
}

public class MultiNestingAccess {
    public static void main(String[] args) {
        MNA mna = new MNA();
        MNA.A mnaa = mna.new A();
        MNA.A.B mnaab = mnaa.new B();
        mnaab.h();
    }
}

为什么需要内部类

1.内部类继承自某个类或者实现某个接口,内部类的代码操作创建它的外围类的对象。所以可以认为内部类提供了某种进入其外围类的窗口。
2.每个内部类都能独立地继承自一个(接口的)实现,所以无论外围类是否已经继承了某个(接口的)实现,对于内部类都没有影响。
内部类是的多重继承的解决方案变得完整。内部类允许继承多个非接口类型。
一种情形:即必须在一个类中以某种方式实现两个接口。由于接口的灵活性,有两种选择:使用单一类,或者使用内部类:

public interface A {
}
public interface B {
}
public class X implements A, B {
}
public class Y implements A {
    B makeB() {
        return new B() {};
    }
}

public class MultiInterfaces {
    static void takesA(A a){}
    static void takesB(B b){}

    public static void main(String[] args) {
        X x = new X();
        Y y = new Y();
        takesA(x);
        takesA(y);
        takesB(x);
        takesB(y.makeB());
    }
}

如果没有任何限制上诉两种方式是一样的。
如果拥有的是抽象类或者具体的类,而不是接口,那就只能使用内部类才能实现多重继承:

public class D {
}
abstract class E {
}
public class Z extends D {
    E makeE() {
        return new E(){};
    }
}
public class MultiImplementation {
    static void takesD(D d){}
    static void takesE(E e){}

    public static void main(String[] args) {
        Z z = new Z();
        takesD(z);
        takesE(z.makeE());
    }
}

使用内部类,还可以获得其他一些特性:
1.内部类可以有多个实例,每个实例都有自己的状态信息,并且与其外围类对象的信息相互独立
2.在单个外围类中,可以让多个内部类以不同的方式实现同一个接口,或继承同一个类。
3.创建内部类对象的时刻并不依赖于外围类对象的创建。
4.内部类并没有令人疑惑的is-a关系,它就是一个独立的实体。

闭包与回调

闭包(closure)是一个可调用的对象,它记录了一些信息,这些信息来自于创建它的作用域。通过这个定义,可以看出内部类是面对对象的闭包,因为它不仅包含外围类对象的信息,还自动拥有一个指向此外围对象的引用,在此作用域内,内部类有权操作所有的成员,包括private成员。
Java中并没有指针以允许回调(callback),通过内部类提供闭包的功能是优良的解决方案,它比指针更灵活,更安全:

public interface Incrementable {
    void increment();
}
public class Callee1 implements Incrementable {
    private int i = 0;
    @Override
    public void increment() {
        i++;
        System.out.println(i);
    }
}
public class Myincrement {
    public void increment(){
        System.out.println("Other operation");
    }
    static void f(Myincrement mi){
        mi.increment();
    }
}
public class Callee2 extends Myincrement{
    private int i = 0;
    @Override
    public void increment() {
        super.increment();
        i++;
        System.out.println(i);
    }
    private class Closure implements Incrementable{
        @Override
        public void increment() {
            Callee2.this.increment();
        }
    }
    Incrementable getCallbackReference() {
        return new Closure();
    }
}
public class Caller {
    private Incrementable callbackReference;
    Caller(Incrementable cbh){
        callbackReference = cbh;
    }
    void go() {
        callbackReference.increment();
    }
}
public class Callbacks {
    public static void main(String[] args) {
        Callee1 c1 = new Callee1();
        Callee2 c2 = new Callee2();
        Myincrement.f(c2);
        Caller caller1 = new Caller(c1);
        Caller caller2 = new Caller(c2.getCallbackReference());
        caller1.go();
        caller1.go();
        caller2.go();
        caller2.go();
    }
}/*
Other operation
1
1
2
Other operation
2
Other operation
3
*/

Callee2继承自MyIncrement,后者已经有一个不同的increment方法,所以不能为了Incrementable的用途而覆盖increment方法,使用内部类独立实现Incrementable。Callee2中除了getCallbackReference()以外,其他成员都是private的。想要建立与外部世界的任何连接,interface Incrementable都是必须的。在这里可以看到,interface是如何允许接口与接口的实现完全独立的。
内部类Closure实现了Incrementable,以提供一个返回Callee2的钩子hook——而且是一个安全的钩子。无论谁获得此Incrementable的引用,都只能调用increment(),除此之外没有其他功能,不想指针那样,允许你做很多事情。
Caller的构造器需要一个Incrementable的引用做参数,然后在以后的某个时刻,Caller对象可以使用此引用回调Callee类。
回调的价值在于它的灵活性——可以在运行时动态的决定需要调用什么方法。

内部类与控制框架

控制框架control framework
应用程序框架application framework就是被设计用以解决某类特定问题的一个类或一组类。要运用某个应用程序框架,通常是继承一个或多个类,并覆盖某些方法。在覆盖后的方法中,编写代码定制应用程序提供的通用解决方法,以解决你的特定问题。
控制框架是一类特殊的应用程序框架,它用来响应事件的需求。主要用来响应事件的系统被称作事件驱动系统。
在事件就绪的时候(基于时间触发的时间)执行事件:
1,首先描述要控制的事件

public abstract class Event {
    private long eventTime;
    protected final long delayTime;
    public Event(long delayTime){
        this.delayTime = delayTime;
        start();
    }
    public void start(){
        eventTime = System.nanoTime() + eventTime;
    }
    public boolean ready(){
        return System.nanoTime() >= eventTime;
    }
    public abstract void action();
}

当希望运行Event并随后调用start时,那么构造器就会捕获时间,此时间是这样得来的:start获取当前时间,然后加上一个延迟时间,这样生成触发事件的时间。start是一个独立的方法,而没有包含在构造器内,因为这样就可以在事件运行之后重新启动计时器,也就是能够重复使用Event对象。
ready告诉你何时可以运行action方法。

public class Controller {
    private List<Event> eventList = new ArrayList<Event>();
    public void addEvent(Event c){
        eventList.add(c);
    }
    public void run(){
        while (eventList.size() > 0){
            for (Event e : new ArrayList<Event>(eventList)){
                if(e.ready()){
                    System.out.println(e);
                    e.action();
                    eventList.remove(e);
                }
            }
        }
    }
}

run方法遍历eventList,寻找就绪的要运行的Event对象。
在目前的设计中你并不知道Event到底做了什么。这正是此设计的关键所在,使变化的事物与不变的事物相互分离。变化向量,就是各种不同的Event对象所具有的不同的行为,而你通过创建不同的Event子类来变现不同的行为。
这正是内部类要做的事情,内部类允许:
1.控制框架的完整实现是由单个的类创建的,从而使得实现的细节被封装起来。内部类用来表示解决问题所必需的各种action。
2.内部类能够很容易的访问外围类的任意成员,所以可以避免这种实现变得笨拙。如果没有这种能力,代码将变得令人讨厌。
考虑此控制框架的一个特定实现,如控制室温运作:控制灯光、水、温度调节器开关以及响铃和重启系统,每个行为都是完全不同的。控制框架的设计使得分离这些不同的代码变得非常容易。使用内部类,可以在单一的类里面产生对同一个基类event的多种导出版本。

public class GreenhouseControllers extends Controller {
    private boolean light = false;
    public class LightOn extends Event{
        public LightOn(long delayTime){
            super(delayTime);
        }
        @Override
        public void action() {
            light = true;
        }
        @Override
        public String toString(){
            return "Light is on";
        }
    }
    public class LightOff extends Event{
        public LightOff(long delayTime){
            super(delayTime);
        }
        @Override
        public void action() {
            light = false;
        }
        @Override
        public String toString(){
            return "Light is off";
        }
    }
    private boolean water =false;
    public class WaterOn extends Event{
        public WaterOn(long delayTime){
            super(delayTime);
        }
        @Override
        public void action() {
            water = true;
        }
        @Override
        public String toString(){
            return "Greenhouse water is on";
        }
    }
    public class WaterOff extends Event{
        public WaterOff(long delayTime){
            super(delayTime);
        }
        @Override
        public void action() {
            water = false;
        }
        @Override
        public String toString(){
            return "Greenhouse water is off";
        }
    }
    private String thermostat = "Day";
    public class ThermostatNight extends Event{
        public ThermostatNight(long delayTime){
            super(delayTime);
        }
        @Override
        public void action() {
            thermostat = "Night";
        }
        @Override
        public String toString(){
            return "ThermostatNight on night setting";
        }
    }
    public class ThermostatDay extends Event{
        public ThermostatDay(long delayTime){
            super(delayTime);
        }
        @Override
        public void action() {
            thermostat = "Day";
        }
        @Override
        public String toString(){
            return "ThermostatNight on day setting";
        }
    }
    public class Bell extends Event{
        public Bell(long delayTime){
            super(delayTime);
        }
        @Override
        public void action() {
            addEvent(new Bell(delayTime));
        }
        @Override
        public String toString(){
            return "Bing!";
        }
    }
    public class Restart extends Event{
        private Event[] eventList;
        public Restart(long delayTime, Event[] eventList){
            super(delayTime);
            this.eventList = eventList;
            for (Event e : eventList){
                addEvent(e);
            }
        }
        @Override
        public void action() {
            for (Event e : eventList){
                e.start();
                addEvent(e);
            }
            start();
            addEvent(this);
        }
        @Override
        public String toString(){
            return "Restarting system";
        }
    }
    public static class Terminate extends Event{
        public Terminate(long delayTime){
            super(delayTime);
        }
        @Override
        public void action() {
            System.exit(0);
        }
        @Override
        public String toString(){
            return "Terminating";
        }
    }
}

Bell控制响铃,然后在事件列表中增加一个Bell对象,于是过一会它可以再次响铃。内部类是多么像多重继承:Bell和Restart有Event的所有方法,并且视乎也拥有外围类的所有方法。
一个有Event对象组成的数组被递交给Restart,该数组要加到控制器上。由于Restart也是一个Event对象,所以同样可以将Restart对象添加到Restart.action()中,以使系统能够有规律地重新启动自己。

public class GreenhouseController {
    public static void main(String[] args) {
        GreenhouseControllers gc = new GreenhouseControllers();
        //gc.addEvent(gc.new Bell(900));
        Event[] eventList = {
          gc.new ThermostatNight(0),
          gc.new LightOn(200),
          gc.new LightOff(400),
          gc.new WaterOn(600),
          gc.new WaterOff(800),
          gc.new ThermostatDay(1400),
        };
        gc.addEvent(gc.new Restart(2000,eventList));
        if(args.length == 1){
            gc.addEvent(new GreenhouseControllers.Terminate(
                    new Integer(args[0])
            ));
        }
        gc.run();
    }
}

这个类的作用是初始化系统,所以他添加了所有对应的事件。Restart事件反复运行,而且他每次都会讲EventList加载到CreenhouseControls对象中。如果提供了命令行参数,系统会以它最为毫秒数,决定什么时候终止程序。

内部类的继承

因为内部类的构造器必须链接其外围对象的引用。那个指向外围类对象的秘密引用必须被初始化,而在导出类中不再存在可以链接的默认对象。必须在构造器内使用enclosingClassReference.super();

public class WithInner {
    class Inner{}
}
public class InheritInner extends WithInner.Inner {
    InheritInner(WithInner wi){
        wi.super();
    }

    public static void main(String[] args) {
        WithInner wi = new WithInner();
        InheritInner ii = new InheritInner(wi);
    }
}

内部类可以被覆盖吗

public class Egg {
    private Yolk y;
    protected class Yolk{
        public Yolk(){
            System.out.println("Egg.Yolk()");
        }
    }
    public Egg(){
        System.out.println("New Egg()");
        y = new Yolk();
    }
}
public class BigEgg extends Egg{
    public class Yolk{
        public Yolk(){
            System.out.println("BigEgg.Yolk()");
        }
    }

    public static void main(String[] args) {
        new BigEgg();
    }
}/*
New Egg()
Egg.Yolk()
*/

当继承了外围类的时候,内部类并没有发生什么变化。这两个内部类是完全独立的两个实体,各自在自己的命名空间内。当然,明确的继承某个内部类也是可以的:

public class Egg2 {
    protected class Yolk{
        public Yolk(){
            System.out.println("Egg2.Yolk()");
        }
        public void f(){
            System.out.println("Egg2.Yolk.f()");
        }
    }
    private Yolk y = new Yolk();
    public Egg2(){
        System.out.println("new Egg2");
    }
    public void insertYolk(Yolk yy){
        y = yy;
    }
    public void g() {
        y.f();
    }
}
public class BigEgg2 extends Egg2{
    public class Yolk extends Egg2.Yolk{
        public Yolk(){
            System.out.println("BigEgg2.Yolk()");
        }
        @Override
        public void f(){
            System.out.println("BigEgg2.Yolk.f()");
        }
    }
    public BigEgg2(){
        insertYolk(new Yolk());
    }

    public static void main(String[] args) {
        Egg2 e2 = new BigEgg2();
        e2.g();
    }
}/*
Egg2.Yolk()
new Egg2
Egg2.Yolk()
BigEgg2.Yolk()
BigEgg2.Yolk.f()
*/

现在BigEgg2.Yolk通过extends Egg2.Yolk明确地继承了此内部类,并且覆盖了其中的方法。insertYolk()方法允许BigEgg2将自己的Yolk对象向上转型为Egg2中的引用y。所以当g()调用y.f()时,覆盖后的新版本f()被执行。第二次调用Egg2.Yolk(),结果BigEgg2.Yolk的构造器调用了其基类的构造器,可以看到在调用g的时候,新版的f被调用了。

局部内部类

局部内部类不能有访问说明符,因为他不是外围类的一部分;但是它可以访问当前代码块的常量,以及此外围类的所有成员。

public interface Counter {
    int next();
}
public class LacalInnerClass {
    private int count = 0;
    Counter getCounter(final String name){
        class LocalCounter implements Counter{
            public LocalCounter(){
                System.out.println("LocalCounter()");
            }
            @Override
            public int next() {
                printnb(name);
                return count++;
            }
        }
        return new LocalCounter();
    }

    private void printnb(String name) {
        System.out.print(name);
    }
    Counter getCounter2(final String name){
       return new Counter() {
           {
               System.out.println("Counter()");
           }
           @Override
           public int next() {
               printnb(name);
               return count++;
           }
       } ;
    }

    public static void main(String[] args) {
        LacalInnerClass lic = new LacalInnerClass();
        Counter
                c1 = lic.getCounter("Local inner "),
                c2 = lic.getCounter2("Anonymous inner ");
        for(int i = 0; i < 5; i++){
            System.out.println(c1.next());
        }
        for (int i = 0; i < 5; i++){
            System.out.println(c2.next());
        }
    }
}/*
LocalCounter()
Counter()
Local inner 0
Local inner 1
Local inner 2
Local inner 3
Local inner 4
Anonymous inner 5
Anonymous inner 6
Anonymous inner 7
Anonymous inner 8
Anonymous inner 9
*/

Counter返回的是序列中的下一个值。分别使用局部内部类和匿名内部类实现了这个功能,它们具有相同的行为和能力。既然局部内部类的名字在方法外是不可见的, 那么为什么不实用匿名内部类?因为需要一个已命名的构造器,或者需要重载构造器,而匿名内部类只能用于实例初始化。另一个理由是,需要不止一个该内部类对象。

内部类标识符

内部类也会生成同普通类一样的class文件:外围类名字加上$,在加上内部类名字:
LocalInnerCalss生成的.class文件包括:

1.Counter.class
2.LocalInnerClass&1.class
3.LocalInnerClass$1LocalCounter.class
4.LocalInnerClass.class

如果内部类是匿名的,编译器会简单的产生一个数字做为标识符。如果内部类是嵌套在别的内部类之中,只需直接将它们的名字加在其外围类标识符与$的后面。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值