AWT事件模型
Coding:
class Time { //
private List lt = new ArrayList();//
private int year; //
public int getYear() { return year; }
public void addTimeListener(TimeListener t) {
lt.add(t);
}
public void removeTimeListener(TimeListener t) {
lt.remove(t);
}
public void action() {
TimeEvent event = new TimeEvent(this);
for(int i=2006;i<=3000;i++) {
this.year = i; //
Iterator it = lt.iterator();
while(it.hasNext()) {
TimeListener tl = (TimeListener)it.next();
// disp()
tl.disp(event);
}
}
}
}
//
class TimeEvent extends EventObject {
public TimeEvent(Object o) {
super(o);
}
}
//
interface TimeListener extends EventListener {
void disp(TimeEvent event);
}
// 1
class Olympics implements TimeListener {
public void disp(TimeEvent event) {
Time t = (Time)event.getSource();
if(t.getYear() % 4 == 0 ){
System.out.println(t.getYear()+” ”);
}
}
}
// 2
AWT事件模型
class WorldCup implements TimeListener {
public void disp(TimeEvent event) {
Time t = (Time)event.getSource();
if(t.getYear() % 4 == 2 ) {
System.out.println(t.getYear()+” ”);
}
}
}
1 N 7
7
(Steven Ye)mailto: leton.ye@