
public class EventNotifier ...{
private InterestingEvent ie;
private boolean somethingHappened;
//Delphi中的写法
//property OnEvent : TOnEvent read FOnEvent write FOnEvent;
public EventNotifier(InterestingEvent event) ...{
this.ie = event;
somethingHappened = true;
}
//Delphi中的写法
//if assigned(FOnEvent) then
//FOnEvent(self,StrLog);
public void doWork() ...{
if (somethingHappened) ...{
ie.interestingEvent();
}
}
//
public static void main(String[] argc)...{
CallMe cm= new CallMe();
EventNotifier ev = new EventNotifier(cm);
ev.doWork();
}
}
//回调函数要用到的接口
interface InterestingEvent ...{
public void interestingEvent();
}
//要实现调用函数,就要实现回 调函数接口(InterestingEvent )
class CallMe implements InterestingEvent ...{
private EventNotifier en;

public CallMe() ...{
en = new EventNotifier(this);
}


public void interestingEvent() ...{
System.out.println("ddd");
}
}
本文介绍了一个使用Java实现的简单事件通知器设计。该设计通过一个名为EventNotifier的类实现,该类接受一个实现了InterestingEvent接口的对象作为参数,并在doWork方法中触发事件。InterestingEvent接口定义了一个interestingEvent方法,当事件发生时被调用。
171万+

被折叠的 条评论
为什么被折叠?



