public interface EventInfo { 接口
void showEventInfo();
}
public class SearchInfo implements EventInfo { 实现类
public SearchInfo() {
}
public void showEventInfo() {
System.out.println("发生了查询事件");
}
}
public class PrintInfo implements EventInfo { 实现类
public PrintInfo() {
}
public void showEventInfo() {
System.out.println("发生了打印事件");
}
}
public class EventNotifier {
private EventInfo ei;
public EventNotifier() {
}
public void setEventInfo(EventInfo ei) {
this.ei = ei;
}
public void doWork() {
ei.showEventInfo();
}
}
public class Test {
public static void main(String[] args) {
PrintInfo pi = new PrintInfo();
SearchInfo si = new SearchInfo();
EventNotifier en = new EventNotifier();
en.setEventInfo(pi);
en.doWork();
en.setEventInfo(si);
en.doWork();
}
}
最后输出:
发生了打印事件
发生了查询事件
这是我在网上看到的例子 但是没看出怎么就回调了呢 ?