对应第7章
子类可以对父类的方法进行重写。
含有抽象(abstract)方法的类必须设成抽象类,但是没有抽象方法的类也可以设成抽象类,设成抽象类可以禁止实例化。
接口:interface
接口就是“纯抽象类”,看成类就行了,接口也可以像普通类那样作函数参数。
接口中的属性默认是static和final,且接口中的final属性不能是“空白final”。
接口中的方法默认是public
例子:
interface Instrument {
int VALUE = 5; // static & final,不建议在接口中定义数据成员
// Cannot have method definitions:
void play(Note n); // Automatically public
void adjust();
}
“伪多重继承”
继承类并调用接口,最后的类既是父类的子类,也是接口的子类
class Hero extends ActionCharacter
implements CanFight, CanSwim, CanFly {
}
如何选择用接口还是抽象类?
接口是最简洁的,所有方法都是抽象方法,而数据属性必须是static final的,接口中不建议有数据成员;
抽象类中可以有非抽象方法(即有方法体),也有数据,所以含有非抽象方法或数据时建议抽象类,其余全采用接口。
接口的继承:
例如有两个父接口As、Bs,现在创建一个子接口:
interface Cs extends As,Bs {
}
内部类
内部类可以看成整个外部类的数据属性。
好建议是:在外部类中写几个对内部类操作的方法,就好像类内部提供对数据的方法一样。
public class Parcel2 {
class Contents {
//
}
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 c = contents();
Destination d = to(dest);
System.out.println(d.readLabel());
}
如果需要用到内部类类型比如Contents,若直接能访问到直接就是Contents,若不能访问到就写成 Parcel2.Contents
内部类可以访问所在外部类中的属性和方法!!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
下面的例子可以看成model、controller的例子
例:
// 把此接口看成一个控制器
interface Selector {
boolean end();
Object current();
void next();
}
// Sequence是整个外部类,里面有数组items,数组分配空间是size,但是里面真正放的个数看add()多少次,也就是next的值
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;
public boolean end() { return i == items.length; }
public Object current() { return items[i]; }
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();
}
}
}
新测试写法
前面建议在每个类中留有main函数用作测试,缺点是额外代码太多,现在有个利用内部类的新方法:
// Bed.java
public class Bed {
public void f() { System.out.println("f()"); }
// 内部测试类,最终版本中不需包含它
public static class Tester {
public static void main(String[] args) {
Bed t = new Bed();
t.f();
}
}
}
内部类:控制框架
例子:温室控制
总结:Event是总体model,之后有它的很多子类子model,Controller是对总model的控制,GreenhouseControls是Controller的子类,里面含有多个内部类且是Event的子类
// -----------------------------------------------Event.java
public abstract class Event {
// 活动总有延迟,比如喊出start()时系统是第3s,如果delayTime是2s,则真正的eventTime是第5s
private long eventTime;
protected final long delayTime;
public Event(long delayTime) {
this.delayTime = delayTime;
start();
}
public void start() { // Allows restarting
eventTime = System.nanoTime() + delayTime;
}
public boolean ready() {
return System.nanoTime() >= eventTime;
}
public abstract void action();
}
// ----------------------------------------------- Controller.java
// 对Event的控制,实质是对Event集合的控制
public class Controller {
// A class from java.util to hold Event objects:
private List<Event> eventList = new ArrayList<Event>();
public void addEvent(Event c) { eventList.add(c); }
public void run() {
while(eventList.size() > 0)
// Make a copy so you're not modifying the list
// while you're selecting the elements in it:
for(Event e : new ArrayList<Event>(eventList))
if(e.ready()) {
System.out.println(e);
e.action();
eventList.remove(e);
}
}
}
//----------------------------------------------- GreenhouseControls.java
GreenhouseControls是集合控制Controller的具体实现,其中包含多个内部类分别是Event的子类
public class GreenhouseControls extends Controller {
private boolean light = false;
public class LightOn extends Event {
public LightOn(long delayTime) { super(delayTime); }
public void action() {
// Put hardware control code here to
// physically turn on the light.
light = true;
}
public String toString() { return "Light is on"; }
}
public class LightOff extends Event {
}
private boolean water = false;
public class WaterOn extends Event {
public WaterOn(long delayTime) { super(delayTime); }
public void action() {
// Put hardware control code here.
water = true;
}
public String toString() {
return "Greenhouse water is on";
}
}
public class WaterOff extends Event {
}
private String thermostat = "Day";
public class ThermostatNight extends Event {
public ThermostatNight(long delayTime) {
super(delayTime);
}
public void action() {
// Put hardware control code here.
thermostat = "Night";
}
public String toString() {
return "Thermostat on night setting";
}
}
public class ThermostatDay extends Event {
}
// An example of an action() that inserts a
// new one of itself into the event list:
// 每次执行Bell类的action()时,都会再把一个新的Bell放到集合中,从而循环执行
public class Bell extends Event {
public Bell(long delayTime) { super(delayTime); }
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);
}
public void action() {
for(Event e : eventList) {
e.start(); // Rerun each event
addEvent(e);
}
start(); // Rerun this Event
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"; }
}
}
// -----------------------------------------------运行的类 GreenhouseController.java
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));
if(args.length == 1)
gc.addEvent(
new GreenhouseControls.Terminate(
new Integer(args[0])));
gc.run();
}
}
上溯与下溯
例:
class Useful {
public void f() {}
public void g() {}
}
class MoreUseful extends Useful {
public void f() {}
public void g() {}
public void u() {}
}
public class RTTI {
public static void main(String[] args) {
Useful[] x = {
new Useful(),
new MoreUseful()
};
x[0].f();
x[1].g();
// 错误
//! x[1].u();
((MoreUseful)x[1]).u();
// 出现Exception thrown
((MoreUseful)x[0]).u();
}
}