第十章 内部类(下)
随着后面所讲的内容越来越深入,所以可能理解得比较慢了,同时这里边的文字描述和示例也越来越多,希望大家能够坚持下去,慢慢看完,相信会有所收获,当然如果像我一样一个字一个字的敲一遍,印象会更深的。不骗你!因为有的东西开始真的不是很清楚,然后自己敲代码做测试的时候敲着敲着就理解了,当然,我并不能完全确认自己理解的意思是否是书中所表达的,但是至少解决了自己的疑惑,相信在以后的工作中如果有机会看到相似的情况时会有更清楚、更深刻的理解。
10.7 嵌套类
如果不需要内部类对象与其外围类对象之间有联系,那么可以将内部类声明为static。这通常称为嵌套类。想要理解static应用于内部类时的含义,就必须记住,普通的内部类对象隐式地保存了一个引用,指向创建它的外围类对象。然而,当内部类是static的时候,就不是这样了。嵌套类意味着:
- 要创建嵌套类的对象,并不需要其外围类的对象。
- 不能从嵌套类的对象中访问非静态的外围类对象。
嵌套类与普通的内部类还有一个区别。普通内部类的字段与方法,只能放在类的外部层次上,所以普通的内部类不能有static数据和static字段,也不能包含嵌套类。但是嵌套类可以包含所有这些东西:
public class Parcel11 {
private static class ParcelContents 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 ParcelContents();
}
public static void main(String[] args) {
Contents c = contents();
Destination d = destination("Tasmania");
}
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 ParcelContents();
}
public static void main(String[] args) {
Contents c = contents();
Destination d = destination("Tasmania");
}
}
在main()中,没有任何Parcel11的对象是必需的;而是使用选取static成员的普通语法来调用方法——这些方法返回对Contents和Destination的引用。
就像你在本章全面看到的那样,在一个普通的(非static)内部类中,通过一个特殊的this引用可以链接到其外围类对象。嵌套类就没有这个特殊的this引用,这使得它类似于一个static方法。
10.7.1 接口内部的类
正常情况下,不能在接口内部放置任何代码,但嵌套类可以作为接口的一部分。你放到接口中的任何类都自动地是public和static的。因为类是static的,只是将嵌套类置于接口的命名空间内,这并不违反接口的规则。你甚至可以在内部类中实现其外围接口,就像下面这样:
public interface ClassInInterface {
void howdy();
class Test implements ClassInInterface{
public void howdy(){
System.out.println("Howdy!");
}
public static void main(String[] args) {
new Test().howdy();
}
}
class Test implements ClassInInterface{
public void howdy(){
System.out.println("Howdy!");
}
public static void main(String[] args) {
new Test().howdy();
}
}
}
运行结果:Howdy!
如果你想要创建某些公共代码,使得它们可以被某个接口的所有不同实现所共用,那么使用接口内部的嵌套类会显得很方便。
我曾在本书中建议过,在每个类中都写一个main()方法,用来测试这个类。这样做有一个缺点,那就是必须带着那些已编译过的额外代码。如果这对你是个麻烦,那就可以使用嵌套类来放置测试代码。
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();
}
}
System.out.println("f()");
}
public static class Tester{
public static void main(String[] args) {
TestBed t=new TestBed();
t.f();
}
}
}
运行结果:f()
这生成了一个独立的类TestBed$Tester(要运行这个程序,执行Java TestBed$Tester即可,在Unix/Linux系统中必须转义$)。可以使用这个类来做测试,但是不必在发布产品中包含它,在将产品打包前可以简单地删除TestBed$Tester.class。
10.7.2 从多层嵌套类中访问外部类的成员
一个内部类被嵌套多少层并不重要——它能透明地访问所有它所嵌入的外围类的所有成员,如下所示:
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 a = mna.new A();
MNA.A.B b = a.new B();
b.h();
}
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 a = mna.new A();
MNA.A.B b = a.new B();
b.h();
}
}
可以看到在MNA.A.B中,调用方法g()和f()不需要任何条件(即使它们被定义为private)。这个例子同时展示了如何从不同的类里创建多层嵌套的内部类对象的基本语法。“.new”语法能产生正确的作用域,所以不必在调用构造器时限定类名。
10.8 为什么需要内部类
至此,我们已经看到了许多描述内部类的语法和语义,但是这并不能回答“为什么需要内部类”这个问题。那么,Sun公司为什么会如此费心地增加这项基本的语言特性呢?
一般说来,内部类继承自某个类或实现某个接口,内部类的代码操作创建它的外围类的对象。所以可以认为内部类提供了某种进入其外围类的窗口。
内部类必须要回答的一个问题是:如果只是需要一个对接口的引用,为什么不通过外围类实现那个接口呢?答案是:“如果这能满足需求,那么就应该这样做。”那么内部类实现一个接口与外围类实现这个接口有什么区别呢?答案是:后者不是总能享用到接口带来的方便,有时需要用到接口的实现。所以,使用内部类最吸引人的原因是:
每个内部类都能独立地继承自一个(接口的)实现,所以无论外围类是否已经继承了某个(接口的)实现,对于内部类都没有影响。
如果没有内部类提供的、可以继承多个具体的或抽象的类的能力,一些设计与编程问题就很难解决。从这个角度看,内部类使得多重继承的解决方案变得完整。接口解决了部分问题,而内部类有效地实现了“多重继承”。也就是说,内部类允许继承多个非接口类型(译注:类或抽象类)。
为了看到更多的细节,让我们考虑这样一种情形:即必须在一个类中以某种方式实现两个接口。由于接口的灵活性,你有两种选择:使用单一类,或者使用内部类:
interface A{}
interface B{}
class X implements A,B{}
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());
}
class X implements A,B{}
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());
}
}
当然,这里假设在两种方式下的代码结构都确实有逻辑意义。然而遇到问题的时候,通常问题本身就能给出某些指引,告诉你是应该使用单一类,还是使用内部类。但如果没有任何其他限制,从实现的观点来看,前面的例子并没有什么区别,它们都能正常运作。
如果拥有的是抽象的类或具体的类,而不是接口,那就只能使用内部类才能实现多重继承。
class D{}
abstract class E{}
class Z extends D{
E makeE(){
return new E(){};
}
}
public class MultiImplementation {
static void takesD(D d){}
static void takesE(E d){}
public static void main(String[] args) {
Z z=new Z();
takesD(z);
takesE(z.makeE());
}
class Z extends D{
E makeE(){
return new E(){};
}
}
public class MultiImplementation {
static void takesD(D d){}
static void takesE(E d){}
public static void main(String[] args) {
Z z=new Z();
takesD(z);
takesE(z.makeE());
}
}
如果不需要解决“多重继承”的问题,那么自然可以用别的方式编码,而不需要使用内部类。但如果使用内部类,还可以获得其他一些特性:
- 内部类可以有多个实例,每个实例都有自己的状态信息,并且与外围类对象的信息相互独立。‘
- 在单个外围类中,可以让多个内部类以不同的方式实现同一个接口,或继承同一个类。稍后就会展示一个这样的例子。
- 创建内部类对象的时刻并不依赖于外围类对象的创建。
- 内部类并没有令人迷惑的“is-a”关系;它就是一个独立的实体。
举个例子,如果Sequence.java不使用内部类,就必须声明“Sequence ”是一个"Selector",对应某个特定的Sequence只能有一个Selector。然而使用内部类很容易就能拥有另个个方法reverseSelector(),用它来生成一个反方向遍历序列的Selector。只有内部类才有这种灵活性。
10.8.1 闭包与回调
闭包是一个可调用的对象,它记录了一些信息,这些信息来自于创建它的作用域。通过这个定义,可以看出内部类是面向对象的闭包,因为它不仅包含外围类对象(创建内部类的作用域)的信息,还自动拥有一个指向此外围类对象的引用,在此作用域内,内部类有权操作所有的成员,包括private成员。
Java最引人争议的问题之一就是,人们认为Java应该包含某种类似指针的机制,以允许回调(callback)。通过回调,对象能够携带一些信息,这些信息允许它在稍后的某个时刻调用初始的对象。稍后将会看到这是一个非常有用的概念。如果回调是通过指针实现的,那么就只能寄希望于程序员不会误用该指针
。然而,读者应该已经了解到,Java更小心仔细,所以没有在语言中包括指针。
通过内部类提供闭包的功能是优良的解决方案,它比指针更灵活、更安全。见下例:
interface Incrementable{
void increment();
}
class Callee1 implements Incrementable{
private int i=0;
public void increment(){
i++;
System.out.println(i);
}
}
class MyIncrement{
public void increment(){
System.out.println("Other operation");
}
static void f(MyIncrement mi){
mi.increment();
}
}
class Callee2 extends MyIncrement{
private int i=0;
public void increment(){
super.increment();
i++;
System.out.println(i);
}
private class Closure implements Incrementable{
public void increment(){
Callee2.this.increment();
}
}
Incrementable getCallbackReference(){
return new Closure();
}
}
class Caller{
private Incrementable callbackReference;
Caller(Incrementable cbh){
callbackReference=cbh;
}
void go(){
callbackReference.increment();
}
}
public class Callbacks {
public static void main(String[] args) {
Callee1 callee1 = new Callee1();
Callee2 callee2 = new Callee2();
MyIncrement.f(callee2);
Caller caller1 = new Caller(callee1);
Caller caller2 = new Caller(callee2.getCallbackReference());
caller1.go();
caller1.go();
caller2.go();
caller2.go();
}
class Callee1 implements Incrementable{
private int i=0;
public void increment(){
i++;
System.out.println(i);
}
}
class MyIncrement{
public void increment(){
System.out.println("Other operation");
}
static void f(MyIncrement mi){
mi.increment();
}
}
class Callee2 extends MyIncrement{
private int i=0;
public void increment(){
super.increment();
i++;
System.out.println(i);
}
private class Closure implements Incrementable{
public void increment(){
Callee2.this.increment();
}
}
Incrementable getCallbackReference(){
return new Closure();
}
}
class Caller{
private Incrementable callbackReference;
Caller(Incrementable cbh){
callbackReference=cbh;
}
void go(){
callbackReference.increment();
}
}
public class Callbacks {
public static void main(String[] args) {
Callee1 callee1 = new Callee1();
Callee2 callee2 = new Callee2();
MyIncrement.f(callee2);
Caller caller1 = new Caller(callee1);
Caller caller2 = new Caller(callee2.getCallbackReference());
caller1.go();
caller1.go();
caller2.go();
caller2.go();
}
}
运行结果:
Other operation
1
1
2
Other operation
2
Other operation
1
1
2
Other operation
2
Other operation
3
这个例子进一步展示了外围类实现一个接口与内部类实现此接口之间的区别。就代码而言,Callee1是简单的解决方式。Callee2继承自MyIncrement,后者已经有了一个不同的increment()方法,并且与Incrementable接口期望的increment()方法完全不相关。所以如果Callee2继承了MyIncement,就不能为了Incrementable的用途而覆盖increment()方法,于是只能使用内部类独立地实现Incrementable。还要注意,当创建了一个内部类时,并没有任何外围类的接口中添加东西,也没有修改外围类的接口。
注意,在Callee2中除了getCallbackReference()以外,其他成员都是private的。要想建立与外部世界的任何连接,interface Incrementable都是必需的。在这里可以看到,interface是如何允许接口与接口的实现完全独立的。
内部类Closure实现了Incrementable,以提供一个返回Callee2的“钩子”(hook)——而且是一个安全的钩子。无论谁获得此Incrementable的引用,都只能调用increment(),除此之外没有其他功能(不像指针那样,允许你做很多事情)。
Caller的构造器需要一个Incrementable的引用作为参数(虽然可以在任意时刻捕获回调引用),然后在以后的某个时刻,Caller对象可以使用此引用回调Callee类。
回调的价值在于它的灵活性——可以在运动时动态地决定需要调用什么方法。这样做的好处在第22章可以看得更明显,在那里实现GUI功能的时候,到处都用到了回调。
10.8.2 内部类与控制框架
在将要介绍的控制框架(control framework)中,可以看到更多使用内部类的具体例子。
应用程序框架(application framework)就是被设计用以解决某类特定问题的一个类或一组类。要运用某个应用程序框架,通常是继承一个或多个类,并覆盖某些方法。在覆盖后的方法中,编写代码定制应用程序框架提供的通用解决方案,以解决你的特定问题(这是设计模式中模版方法的一个例子)。模板方法包含算法的基本结构,并且会调用一个或多个可覆盖的方法,以完成算法的动作。设计模式总是将变化的事物与保持不变的事物分离开,在这个模式中,模板方法是保持不变的事物,而可覆盖的方法就是变化的事物。
控制框架是一类特殊的应用程序框架,它用来解决响应事件的需求。主要用来响应事件的系统被称作事件驱动系统。应用程序设计中常见的问题之一是图形用户接口(GUI),它几乎完全是事件驱动的系统。在第22章将会看到,Java Swing库就是一个控制框架,它优雅地解决了GUI的问题,并使用了大量的内部类。
要理解内部类是如果运行简单的创建过程已经如何使用控制框架的,请考虑这样一个控制框架,它的工作就是在事件“就绪”的时候执行事件。虽然“就绪”可以指任何事,但在本例中是指基于时间触发的事件。接下来的问题就是,对于要控制什么,控制框架并不包含任何具体的信息。那些信息是在实现算法的action()部分时,通过继承来提供的。
首先,接口描述了要控制的事件。因为其默认的行为是基于时间去执行控制,所以使用抽象类代替实际的接口。下面的例子包含了某些实现:
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()+delayTime;
}
public boolean ready(){
return System.nanoTime()>=eventTime;
}
public abstract void action();
protected final long delayTime;
public Event(long delayTime){
this.delayTime=delayTime;
start();
}
public void start(){
eventTime=System.nanoTime()+delayTime;
}
public boolean ready(){
return System.nanoTime()>=eventTime;
}
public abstract void action();
}
当希望运行Eent并随后调用start()时,那么构造器就会捕获(从对象创建的时刻开始的)时间,此时间是这样得来的:start()获取当前时间,然后加上一个延迟时间,这样生成触发时间的时间。start()是一个独立的方法,而没有包含在构造器内,因为这样就可以在事件运行以后重新启动计时器,也就是能够重复使用Eent对象。例如,如果想要重复一个事件,只需简单地在action()中调用start()方法。
ready()告诉你何时可以运行action()方法了。当然,可以在导出类中覆盖ready()方法,使得Eent能够基于时间以外的其他因素而触发。
下面的文件包含了一个用来管理并触发事件的实际控制框架。Event对象被保存在List<Event>类型(读作“Event的列表”)的容器对象中,容器会在第11章中详细介绍。目前读者只需要知道add()方法用来将一个Object添加到List的尾端,size()方法用来得到List中元素的个数,foreach语法用来连续获联List中的Event,remove()方法用来从List中移除指定的Event。
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);
}
}
}
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,寻找就绪的(ready())、要运行的Event对象。对找到的每一个就绪的(ready())事件,使用对象的toString()打印其信息,调用其action()方法,然后从队列中移除此Event。
注意,在目前的设计中你并不知道Event到底做了什么。这正是此设计的关键所在,“使变化的事物与不变的事物相互分离”。用我的话说,“变化向量”就是各种不同的Event对象所具有的不同行为,而你通过创建不同的Event子类来表现不同的行为。
这正是内部类要做的事情,内部类允许:
- 控制框架的完整实现是由单个的类创建的,从而使得实现的细节被封装了起来。内部类用来表示解决问题所必需的各种不同的action()。
- 内部类能够很容易地访问外围类的任意成员,所以可以避免这种实现变得笨拙。如果没有这种能力,代码将变得令人讨厌,以至于你肯定会选择别的方法。
考虑此控制框架的一个特定实现,如控制温室的运作:控制灯光、水、温度调节器的开关,以及响铃和重新启动系统,每个行为都是完全不同的。控制框架的设计使得分离这些不同的代码变得非常容易。使用内部类,可以在单一的类里面产生对同一个基类Event的多种导出版本。对于温室系统的每一种行为,都继承一个新的Event内部类,并在要实现的action()中编写控制代码。
public class GreenhouseControls extends Controller{
private boolean light=false;
public class LightOn extends Event{
public LightOn(long delayTime) {
super(delayTime);
}
@Override
public void action() {
light=true;
}
public String toString(){
return "Light is on";
}
}
public class LightOff extends Event{
public LightOff(long delayTime) {
super(delayTime);
}
@Override
public void action() {
light=false;
}
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;
}
public String toString(){
return "Greenhouse water is on";
}
}
public class WaterOff extends Event{
public WaterOff(long delayTime) {
super(delayTime);
}
@Override
public void action() {
water=true;
}
public String toString(){
return "Greenhouse water is on";
}
}
private String thermostat="Day";
public class ThermostatNight extends Event{
public ThermostatNight(long delayTime) {
super(delayTime);
}
@Override
public void action() {
thermostat="Night";
}
public String toString(){
return "Thermostat on night setting";
}
}
public class ThermostatDay extends Event{
public ThermostatDay(long delayTime) {
super(delayTime);
}
@Override
public void action() {
thermostat="Day";
}
public String toString(){
return "Thermostat on day setting";
}
}
public class Bell extends Event{
public Bell(long delayTime) {
super(delayTime);
}
@Override
public void action() {
addEvent(new Bell(delayTime));
}
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);
}
public String toString(){
return "Restarting system";
}
}
public static class Terminate extends Event{
public Terminate(long delayTime){
super(delayTime);
}
public void action(){
System.exit(0);
}
public String toString(){
return "Terminating";
}
}
public class LightOn extends Event{
public LightOn(long delayTime) {
super(delayTime);
}
@Override
public void action() {
light=true;
}
public String toString(){
return "Light is on";
}
}
public class LightOff extends Event{
public LightOff(long delayTime) {
super(delayTime);
}
@Override
public void action() {
light=false;
}
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;
}
public String toString(){
return "Greenhouse water is on";
}
}
public class WaterOff extends Event{
public WaterOff(long delayTime) {
super(delayTime);
}
@Override
public void action() {
water=true;
}
public String toString(){
return "Greenhouse water is on";
}
}
private String thermostat="Day";
public class ThermostatNight extends Event{
public ThermostatNight(long delayTime) {
super(delayTime);
}
@Override
public void action() {
thermostat="Night";
}
public String toString(){
return "Thermostat on night setting";
}
}
public class ThermostatDay extends Event{
public ThermostatDay(long delayTime) {
super(delayTime);
}
@Override
public void action() {
thermostat="Day";
}
public String toString(){
return "Thermostat on day setting";
}
}
public class Bell extends Event{
public Bell(long delayTime) {
super(delayTime);
}
@Override
public void action() {
addEvent(new Bell(delayTime));
}
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);
}
public String toString(){
return "Restarting system";
}
}
public static class Terminate extends Event{
public Terminate(long delayTime){
super(delayTime);
}
public void action(){
System.exit(0);
}
public String toString(){
return "Terminating";
}
}
}
注意,light、water和thermostat都属于外围类GreenhouseControl,而这些内部类能够自由地访问那些字段,无需限定条件或特殊许可。而且,action()方法通常都涉及对某种硬件的控制。
大多数Event类看起来都很相似,但是Bell和Restart则比较特别。Bell控制响铃,然后在事件列表中增加一个Bell对象,于是过一会儿它可以再次响铃。读者可能注意到了内部类是多么像多重继承:Bell和Restart有Event所有方法,并且似乎也拥有外围类GreenhouseContrlos的所有方法。
一个由Event对象组成的数组被递交给Restart,该数组要加到控制器上。由于Restart()也是一个Event对象,所以同样可以将Restart对象添加到Restart.action()中,以使系统能够有规律地重新启动自己。
下面的类通过创建一个GreenhouseControls对象,并添加各种不同的Event对象来配置该系统。这是命令设计模式的一个例子在eventList中的每一个被封装成对象的请求:
public class GreenhouseController {
public static void main(String[] args) {
GreenhouseControls gc = new GreenhouseControls();
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));
GreenhouseControls gc = new GreenhouseControls();
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 GreenhouseControls.Terminate(new Integer(args[0])));
gc.addEvent(new GreenhouseControls.Terminate(1000));
gc.run();
// }
}
gc.run();
// }
}
}
注:被我注释掉的代码是书中写的,我发现args没有被初始化所以会报空指针,因此被我修改了一下
运行结果:
Bing!
Thermostat on night setting
Light is on
Light is off
Greenhouse water is on
Greenhouse water is on
Thermostat on day setting
Restarting system
Thermostat on night setting
Light is on
Light is off
Greenhouse water is on
Greenhouse water is on
Thermostat on day setting
Restarting system
Terminating
这个类的作用是初始化系统,所以它添加了所有相应的事件。Restart事件反复运行,而且它每次都会将eventList加载到GreenhouseControls对象中。如果提供了命令行参数,系统会以它作为毫秒数,决定什么时候终止程序(这是测试程序时使用的)。当然,更灵活的方法是避免对事件进行硬编码,取而代之的是从文件中读取需要的事件(第12章的练习会要求读者照此方法修改这个例子)。
这个例子应该使读者更了解内部类的价值了,特别是在控制框架中使用内部类的时候。在第18章中,读者将看到内部类如何优雅地描述图形用户界面的行为。到那时,读者应该就完全信服内部类的价值了。
10.9 内部类的继承
因为内部类的构造器必须连接到指向其外围类对象的引用,所以在继承内部类的时候,事情会变得有点复杂。问题在于,那个指向外围类对象的“秘密的”引用必须被初始化,而在导出类中不再存在可连接的默认对象。要解决这个问题,必须使用特殊的语法来明确说清它们之间的关联:
class WithInner{
class Inner{}
}
public class InheritInner extends WithInner.Inner{
// InheritInner(){}
InheritInner(WithInner wi){
wi.super();
}
public static void main(String[] args) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
}
public class InheritInner extends WithInner.Inner{
// InheritInner(){}
InheritInner(WithInner wi){
wi.super();
}
public static void main(String[] args) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
}
可以看到,InheritInner只继承自内部类,而不是外围类。但是当要生成一个构造器时,默认的构造器并不算好,而且不能只是传递一个指向外围类对象的引用。此外,必须在构造器内使用如下语法:
enclosingClassReference.super();
这样才提供了必要的引用,然后程序才能编译通过。
10.10 内部类可以被覆盖吗
如果创建了一个内部类,然后继承其外围类并重新定义此内部类时,会发生什么呢?也就是说,内部类可以被覆盖吗?这看起来似乎是个很有用的思想,但是“覆盖”内部类就好像它是外围类的一个方法,其实并不起什么作用:
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();
}
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();
}
}
默认的构造器是编译器自动生成的,这里是调用基类的默认构造器。你可能认为既然创建了BigEgg的对象,那么所使用的应该是“覆盖后”的Yolk版本,但从输出中可以看到实际情况并不是这样的。
这个例子说明,当继承了某个外围类的时候,内部类并没有发生什么特别神奇的变化。这两个内部类是完全独立的两个实体,各自在自己的命名空间内。当然,明确地继承某个内部类也是可以的:
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()");
}
public void f(){
System.out.println("BigEgg2.Yolk.f()");
}
}
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()");
}
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();
}
}
public static void main(String[] args) {
Egg2 e2 = new BigEgg2();
e2.g();
}
}
运行结果:
Egg2.Yolk()
New Egg2()
Egg2.Yolk()
BigEgg2.Yolk()
New Egg2()
Egg2.Yolk()
BigEgg2.Yolk()
BigEgg2.Yolk.f()
注:这里提示一下,为什么会先输出“Egg2.Yolk() ”,因为执行Egg2 e2 = new BigEgg2(); 时会先实例化Egg类,而该类中会先执行private Yolk y=new Yolk();句。这个是在之前继承方面的知识。我当时没反应过来,看来得温故一下之前的内容了。。。
现在BigEgg2.Yolk通过extends Egg2.Yolk明确地继承了此内部类,并且覆盖了其中的方法。insertYolk()方法允许BigEgg2将它自己的Yolk对象向上转型为Egg2中的引用y。所以当g()调用y.f()时,覆盖后的新版的f()被执行。第二次调用Egg2.Yolk(),结果是BigEgg2.Yolk的构造器调用了其基类的构造器。可以看到在调用g()的时候,新版的f()被调用了。
10.11 局部内部类
前面提到过,可以在代码块里创建内部类,典型的方式是在一个方法体的里面创建。局部内部类不能有访问说明符,因为它不是外围类的一部分,但是它可以访问当前代码块内的常量,以及此外围类的所有成员。下面的例子对局部内部类与匿名内部类的创建进行了比较。
interface Counter{
int next();
}
public class LocalInnerClass {
private int count=0;
Counter getCounter(final String name){
class LocalCounter implements Counter{
public LocalCounter(){
System.out.println("LocalCounter()");
}
@Override
public int next() {
System.out.print(name);
return count++;
}
}
return new LocalCounter();
}
Counter getCounter2(final String name){
return new Counter(){
{
System.out.println("Counter()");
}
@Override
public int next() {
System.out.print(name);
return count++;
}
};
}
public static void main(String[] args) {
LocalInnerClass lic = new LocalInnerClass();
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());
}
}
}
public class LocalInnerClass {
private int count=0;
Counter getCounter(final String name){
class LocalCounter implements Counter{
public LocalCounter(){
System.out.println("LocalCounter()");
}
@Override
public int next() {
System.out.print(name);
return count++;
}
}
return new LocalCounter();
}
Counter getCounter2(final String name){
return new Counter(){
{
System.out.println("Counter()");
}
@Override
public int next() {
System.out.print(name);
return count++;
}
};
}
public static void main(String[] args) {
LocalInnerClass lic = new LocalInnerClass();
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
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返回的是序列中的下一个值。我们分别使用局部内部类和匿名内部类实现了这个功能,它们具有相同的行为和能力。既然局部内部类的名字在方法外是不可见的,那为什么我们仍然使用局部内部类而不是匿名内部类呢?唯一的理由是,我们需要一个已命名的构造器,或者需要重载构造器,而匿名内部类只能用于实例初始化。
所以使用局部内部类而不使用匿名内部类的另一个理由就是,需要不止一个该内部类的对象。
10.12 内部类标识符
由于每个类都会产生一个.class文件,其中包含了如何创建该类型的对象的全部信息(此信息产生一个“meta-class”,叫做Class对象),你可能猜到了,内部类也必须生成一个.class 文件以包含它们的Class对象信息。这些类文件的命名有严格的规则:外围类的名字,加上“$”,再加上内部类的名字。例如,LocalInnerClass.java生成的.class文件包括:
Counter.class
LocalInnerClass$1.class
LocalInnerClass$1LocalCounter.class
LocalInnerClass.class
如果内部类是匿名的,编译器会简单地产生一个数字作为其标识符。如果内部类是嵌套在别的内部类之中,只需直接将它们的名字加在其外围类标识符与“$”的后面。
虽然这种命名格式简单而直接,但它还是很健壮的,足以应对绝大多数情况。因为这是Java的标准命名方式,所以产生的文件自动都是平台无关的。(注意,为了保证你的内部类能起作用,Java编译器会尽可能地转换它们。)
10.13 总结
比起面向对象编程中其他的概念来,接口和内部类更深奥复杂;比如C++就没有这些。将两者结合起来,同样能够解决C++中的多重继承所能解决的问题。然而,多重继承在C++中被证明是相当难以使用的,相比较而言,Java的接口和内部类就容易理解多了。
虽然这些特性本身是相当直观的,但是就像多态机制一样,这些特性的使用应该是设计阶段考虑的问题。随着时间的推移,读者能够更好地识别什么情况下应该使用接口,什么情况使用内部类,或者两者同时使用。但此时,读者至少应该已经完全理解了它们的语法和语义。当见到这些语言特性实际应用时,就最终理解它们了。