public interface InterestingEvent {
// 这只是一个普通的方法,可以接收参数、也可以返回值
public void interestingEvent();
}
class EventNotifier {
private InterestingEvent ie;
private boolean somethingHappened;
public EventNotifier(InterestingEvent event) {
ie = event;
somethingHappened = false;
}
public void doWork() {
if (somethingHappened) {
// 事件发生时,通过调用接口的这个方法来通知
ie.interestingEvent();
}
}
}
public class CallMe implements InterestingEvent {
private EventNotifier en;
public CallMe() {
// 新建一个事件通知者对象,并把自己传递给它
en = new EventNotifier(this);
}
// 实现事件发生时,实际处理事件的方法
public void interestingEvent() {
// 这个事件发生了,进行处理
}
}
interface InterestingEvent {
public void interestingEvent(String event);
}
class CallMe implements InterestingEvent {
private String name;
public CallMe(String name){
this.name = name;
}
public void interestingEvent(String event) {
System.out.println(name + ":[" +event + "] happened");
}
}
class EventNotifier {
private List<CallMe> callMes = new ArrayList<CallMe>();
public void regist(CallMe callMe){
callMes.add(callMe);
}
public void doWork(){
for(CallMe callMe: callMes) {
callMe.interestingEvent("sample event");
}
}
}
public class CallMeTest {
public static void main(String[] args) {
EventNotifier ren = new EventNotifier();
CallMe a = new CallMe("CallMe A");
CallMe b = new CallMe("CallMe B");
// regiest
ren.regist(a);
ren.regist(b);
// test
ren.doWork();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有