Java 事件触发接口回调方法
2011-09-02 16:52
369 查看
其技巧就是:定义一个简单接口,并在该接口中声明我们要调用的方法,一般可以应用在键盘鼠标事件跟踪。
下面举一个例子:
假定我们希望在某个事件发生时得到通知。我们可以定义一个接口:
/*
* 在某个事件发生时得到通知.
*/
public interface InterestingEvent {
public void interestingEvent(); //其它方法体及方法定义也可.
}
此接口中的方法,是个没有返回值的也没有任何参数,如果您愿意也可以有返回值,也可以带参数.这就要看具体需求而定.
这使得我们可以控制实现该接口的类的任何对象。因此,我们不必关心任何外部类型信息。与在将 C++ 代码用于Motif 时使用窗口小部件的数据域来容纳对象指针的难以控制的 C 函数相比,这种方法要好得多。
实现接口的代码如下:
public class CallMe implements InterestingEvent {
public CallMe() {
}
public void interestingEvent() {
System.out.println("发生了CallMe事件,哈哈");
}
}
public class CallYou implements InterestingEvent {
public CallYou() {
}
public void interestingEvent() {
System.out.println("发生了CallYou事件,哈哈");
}
}
发出事件信号的类必须等待实现了 InterestingEvent 接口的对象,并在适当时候调用 interestingEvent() 方法。
public class EventNotifier {
private InterestingEvent ie; //接口对象引用
private boolean somethingHappened ;
public EventNotifier() {
somethingHappened = true ;
}
public void setInterestingEvent(InterestingEvent ie){ //参数引用附值
this.ie = ie ;
}
public void doWork(){
if(somethingHappened){
ie.interestingEvent();
}
}
}
下面做一下测试.
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
CallMe cm = new CallMe();
CallYou cy = new CallYou();
EventNotifier en = new EventNotifier();
en.setInterestingEvent(cm);
en.doWork();
en.setInterestingEvent(cy);
en.doWork();
}
}
此测试在发生指定的调用CalMe事件时,就扫行CallMe下的命令,如发生CallYou事件时,就调用CallYou下的命令.此种方法可以结合Command模式.实现MS-Windows 和 X Window System 事件驱动编程模型.
实际上采用了接口回调引用对象类,从而此对象实现方法调用.
下面举一个例子:
假定我们希望在某个事件发生时得到通知。我们可以定义一个接口:
/*
* 在某个事件发生时得到通知.
*/
public interface InterestingEvent {
public void interestingEvent(); //其它方法体及方法定义也可.
}
此接口中的方法,是个没有返回值的也没有任何参数,如果您愿意也可以有返回值,也可以带参数.这就要看具体需求而定.
这使得我们可以控制实现该接口的类的任何对象。因此,我们不必关心任何外部类型信息。与在将 C++ 代码用于Motif 时使用窗口小部件的数据域来容纳对象指针的难以控制的 C 函数相比,这种方法要好得多。
实现接口的代码如下:
public class CallMe implements InterestingEvent {
public CallMe() {
}
public void interestingEvent() {
System.out.println("发生了CallMe事件,哈哈");
}
}
public class CallYou implements InterestingEvent {
public CallYou() {
}
public void interestingEvent() {
System.out.println("发生了CallYou事件,哈哈");
}
}
发出事件信号的类必须等待实现了 InterestingEvent 接口的对象,并在适当时候调用 interestingEvent() 方法。
public class EventNotifier {
private InterestingEvent ie; //接口对象引用
private boolean somethingHappened ;
public EventNotifier() {
somethingHappened = true ;
}
public void setInterestingEvent(InterestingEvent ie){ //参数引用附值
this.ie = ie ;
}
public void doWork(){
if(somethingHappened){
ie.interestingEvent();
}
}
}
下面做一下测试.
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
CallMe cm = new CallMe();
CallYou cy = new CallYou();
EventNotifier en = new EventNotifier();
en.setInterestingEvent(cm);
en.doWork();
en.setInterestingEvent(cy);
en.doWork();
}
}
此测试在发生指定的调用CalMe事件时,就扫行CallMe下的命令,如发生CallYou事件时,就调用CallYou下的命令.此种方法可以结合Command模式.实现MS-Windows 和 X Window System 事件驱动编程模型.
实际上采用了接口回调引用对象类,从而此对象实现方法调用.
相关文章推荐
- Java 事件触发接口回调方法
- java事件监听机制中的事件、监听器接口、处理方法、事件源
- Android中封装View提供接口供点击事件回调的方法及使用
- java多线程之接口回调、join方法详解
- ZooKeeper系统模型之Watcher——数据变更的通知(接口、事件、回调方法)。
- Android java回调机制与自定义接口回调方法
- Java 是如何利用接口避免函数回调的方法
- anroid 学习之java回调机制与自定义接口回调方法的使用
- java 接口方法的回调
- Java按时间梯度实现异步回调接口的方法
- android java 回调方法接口
- uiview类里面进行触发按钮事件来让viewcontroller进行跳转的方法
- Java反射03 : 获取Class的注解、修饰符、父类、接口、字段、构造器和方法
- 《WF编程》系列之18 - 触发事件与调用方法:服务合同 Service Contracts 3.2.3 触发事件与调用方法
- java中,一个类实现某个接口,必须重写接口中的所有方法吗
- 在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口
- jQuery实现长按按钮触发事件的方法
- Java面向对象05-抽象方法,抽象类和接口
- java SortedSet接口swap方法
- 在workflow中,无法为实例 ID“...”传递接口类型“...”上的事件“...” 问题的解决方法。