按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
…………………………………………………………Page 196……………………………………………………………
④:由于某些特殊原因,这对我来说是一个经常需要解决的、非常有趣的问题;原来的例子在《C++ Inside
& Out》一书里也出现过,但 Java 提供了一种更令人舒适的解决方案。
作为应用程序框架的一种典型行为,GreenhouseControls 类是从 Controller 继承的:
//: GreenhouseControls。java
// This produces a specific application of the
// control system; all in a single class。 Inner
// classes allow you to encapsulate different
// functionality for each type of event。
package c07。controller;
public class GreenhouseControls
extends Controller {
private boolean light = false;
private boolean water = false ;
private String thermostat = 〃Day〃;
private class LightOn extends Event {
public LightOn(long eventTime) {
super(eventTime);
}
public void action() {
// Put hardware control code here to
// physically turn on the light。
light = true;
}
public String description() {
return 〃Light is on〃;
}
}
private class LightOff extends Event {
public LightOff(long eventTime) {
super(eventTime);
}
public void action() {
// Put hardware control code here to
// physically turn off the light。
light = false;
}
public String description() {
return 〃Light is off〃;
}
}
private class WaterOn extends Event {
public WaterOn(long eventTime) {
super(eventTime);
}
public void action() {
// Put hardware control code here
water = true;
}
195
…………………………………………………………Page 197……………………………………………………………
public String description() {
return 〃Greenhouse water is on〃;
}
}
private class WaterOff extends Event {
public WaterOff(long eventTime) {
super(eventTime);
}
public void action() {
// Put hardware control code here
water = false;
}
public String description() {
return 〃Greenhouse water is off〃;
}
}
private class ThermostatNight extends Event {
public ThermostatNight(long eventTime) {
super(eventTime);
}
public void action() {
// Put hardware control code here
thermostat = 〃Night〃;
}
public String description() {
return 〃Thermostat on night setting〃;
}
}
private class ThermostatDay extends Event {
public ThermostatDay(long eventTime) {
super(eventTime);
}
public void action() {
// Put hardware control code here
thermostat = 〃Day〃;
}
public String description() {
return 〃Thermostat on day setting〃;
}
}
// An example of an action() that inserts a
// new one of itself into the event list:
private int rings;
private class Bell extends Event {
public Bell(long eventTime) {
super(eventTime);
}
public void action() {
// Ring bell every 2 seconds; rings times:
System。out。println(〃Bing!〃);
if(……rings 》 0)
addEvent(new Bell(
196
…………………………………………………………Page 198……………………………………………………………
System。currentTimeMillis() + 2000));
}
public String description() {
return 〃Ring bell〃;
}
}
private class Restart extends Event {
public Restart(long eventTime) {
super(eventTime);
}
public void action() {
long tm = System。currentTimeMillis();
// Instead of hard…wiring; you could parse
// configuration information from a text
// file here:
rings = 5;
addEvent(new ThermostatNight(tm));
addEvent(new LightOn(tm + 1000));
addEvent(new LightOff(tm + 2000));
addEvent(new WaterOn(tm + 3000));
addEvent(new WaterOff(tm + 8000));
addEvent(new Bell(tm + 9000));
addEvent(new ThermostatDay(tm + 10000));
// Can even add a Restart object!
addEvent(new Restart(tm + 20000));
}
public String description() {
return 〃Restarting system〃;
}
}
public static void main(String'' args) {
GreenhouseControls gc =
new GreenhouseControls();
long tm = System。currentTimeMillis();
gc。addEvent(gc。new Restart(tm));
gc。run();
}
} ///:~
注意 light (灯光)、water (供水)、thermostat (调温)以及rings 都隶属于外部类
GreenhouseControls ,所以内部类可以毫无阻碍地访问那些字段。此外,大多数action()方法也涉及到某些
形式的硬件控制,这通常都要求发出对非 Java 代码的调用。
大多数Event 类看起来都是相似的,但 Bell (铃)和Restart (重启)属于特殊情况。Bell 会发出响声,若
尚未响铃足够的次数,它会在事件列表里添加一个新的Bell 对象,所以以后会再度响铃。请注意内部类看起
来为什么总是类似于多重继承:Bell 拥有Event 的所有方法,而且也拥有外部类GreenhouseControls 的所
有方法。
Restart 负责对系统进行初始化,所以会添加所有必要的事件。当然,一种更灵活的做法是避免进行“硬编
码”,而是从一个文件里读入它们(第 10章的一个练习会要求大家修改这个例子,从而达到这个目标)。由
于Restart()仅仅是另一个 Event 对象,所以也可以在Restart。action()里添加一个 Restart 对象,使系统
能够定期重启。在main()中,我们需要做的全部事情就是创建一个 GreenhouseControls 对象,并添加一个
Restart 对象,令其工作起来。
这个例子应该使大家对内部类的价值有一个更加深刻的认识,特别是在一个控制框架里使用它们的时候。此
197
…………………………………………………………Pag