Java接口 的理解和使用方法
来源:互联网 发布:我国技术贸易数据 编辑:程序博客网 时间:2024/05/18 01:40
摘要:
Java接口提供了一个很好的方法来实现回调函数。假如你习惯于在事件驱动的编程模型中,通过传递函数指针来调用方法达到目的的话,那么你就会喜欢这个技巧。
作者:John D. Mitchell
在MS-Windows或者X-Window系统的事件驱动模型中,当某些事件发生的时候,开发人员已经熟悉通过传递函数指针来调用处理方法。而在Java的面向对象的模型中,不能支持这种方法,因而看起来似乎排除了使用这种比较舒适的机制,但事实并非如此。
Java的接口提供了一种很好的机制来让我们达到和回调相同的效果。这个诀窍就在于定一个简单的接口,在接口之中定义一个我们希望调用的方法。
举个例子来说,假设当一个事件发生的时候,我们想它被通知,那么我们定义一个接口:
public interface InterestingEvent
{
// This is just a regular method so it can return something or
// take arguments if you like.
public void interestingEvent ();
}
这就给我们一个控制实现了该接口的所有类的对象的控制点。因此,我们不需要关心任何和自己相关的其它外界的类型信息。这种方法比C函数更好,因为在C++风格的代码中,需要指定一个数据域来保存对象指针,而Java中这种实现并不需要。
发出事件的类需要对象实现InterestingEvent接口,然后调用接口中的interestingEvent ()方法。
public class EventNotifier
{
private InterestingEvent ie;
private boolean somethingHappened;
public EventNotifier (InterestingEvent event)
{
// Save the event object for later use.
ie = event;
// Nothing to report yet.
somethingHappened = false;
}
//...
public void doWork ()
{
// Check the predicate, which is set elsewhere.
if (somethingHappened)
{
// Signal the even by invoking the interface's method.
ie.interestingEvent ();
}
//...
}
// ...
}
在这个例子中,我们使用了somethingHappened这个标志来跟踪是否事件应该被激发。在许多事例中,被调用的方法能够激发interestingEvent()方法才是正确的。
希望收到事件通知的代码必须实现InterestingEvent接口,并且正确的传递自身的引用到事件通知器。
public class CallMe implements InterestingEvent
{
private EventNotifier en;
public CallMe ()
{
// Create the event notifier and pass ourself to it.
en = new EventNotifier (this);
}
// Define the actual handler for the event.
public void interestingEvent ()
{
// Wow! Something really interesting must have occurred!
// Do something...
}
//...
}
希望这点小技巧能给你带来方便。
关于作者:
输出结果:
**** 回调 ICallBackOne 开始...****
调用 回调接口one中的方法
*****回调 ICallBackOne 结束。*****
###### 回调 ICallBackTwo 开始...######
调用 回调接口 Two 中的方法 张国明 的年龄是: 28
#######回调 ICallBackTwo 结束。#######
Java接口提供了一个很好的方法来实现回调函数。假如你习惯于在事件驱动的编程模型中,通过传递函数指针来调用方法达到目的的话,那么你就会喜欢这个技巧。
作者:John D. Mitchell
在MS-Windows或者X-Window系统的事件驱动模型中,当某些事件发生的时候,开发人员已经熟悉通过传递函数指针来调用处理方法。而在Java的面向对象的模型中,不能支持这种方法,因而看起来似乎排除了使用这种比较舒适的机制,但事实并非如此。
Java的接口提供了一种很好的机制来让我们达到和回调相同的效果。这个诀窍就在于定一个简单的接口,在接口之中定义一个我们希望调用的方法。
举个例子来说,假设当一个事件发生的时候,我们想它被通知,那么我们定义一个接口:
public interface InterestingEvent
{
// This is just a regular method so it can return something or
// take arguments if you like.
public void interestingEvent ();
}
这就给我们一个控制实现了该接口的所有类的对象的控制点。因此,我们不需要关心任何和自己相关的其它外界的类型信息。这种方法比C函数更好,因为在C++风格的代码中,需要指定一个数据域来保存对象指针,而Java中这种实现并不需要。
发出事件的类需要对象实现InterestingEvent接口,然后调用接口中的interestingEvent ()方法。
public class EventNotifier
{
private InterestingEvent ie;
private boolean somethingHappened;
public EventNotifier (InterestingEvent event)
{
// Save the event object for later use.
ie = event;
// Nothing to report yet.
somethingHappened = false;
}
//...
public void doWork ()
{
// Check the predicate, which is set elsewhere.
if (somethingHappened)
{
// Signal the even by invoking the interface's method.
ie.interestingEvent ();
}
//...
}
// ...
}
在这个例子中,我们使用了somethingHappened这个标志来跟踪是否事件应该被激发。在许多事例中,被调用的方法能够激发interestingEvent()方法才是正确的。
希望收到事件通知的代码必须实现InterestingEvent接口,并且正确的传递自身的引用到事件通知器。
public class CallMe implements InterestingEvent
{
private EventNotifier en;
public CallMe ()
{
// Create the event notifier and pass ourself to it.
en = new EventNotifier (this);
}
// Define the actual handler for the event.
public void interestingEvent ()
{
// Wow! Something really interesting must have occurred!
// Do something...
}
//...
}
希望这点小技巧能给你带来方便。
关于作者:
John D. Mitchell在过去的九年内一直做顾问,曾经在Geoworks使用OO汇编语言开发了PDA软件,爱好于写编译器,Tcl/Tk和Java系统。和人合著了《Making Sense of Java》,目前从事Java编译器的工作。
java回调接口,即接口回调,我理解为:方法methodXX的参数为接口Ixx,当我们调用方法methodXX的时候就可以动态的实现接口Ixx中的方法了。
参照如下代码:
- /**
- * 无参数的回调接口
- *
- * @author 张国明 guomingzhang2008@gmail.com <br/>
- * 2012-8-19 下午7:59:31
- *
- */
- public interface ICallBackOne {
- /**
- * 执行回调操作的方法
- */
- void execute();
- }
- /**
- * 有参数的回调接口
- *
- * @author 张国明 guomingzhang2008@gmail.com <br/>
- * 2012-8-19 下午10:19:13
- *
- */
- public interface ICallBackTwo {
- /**
- * 执行回调操作的方法
- *
- * @param name
- * name
- * @param age
- * age
- */
- void execute(String name, int age);
- }
- /**
- * 回调接口的实现方法
- *
- * @author 张国明 guomingzhang2008@gmail.com <br/>
- * 2012-8-19 下午10:13:50
- *
- */
- public class CallBackImpl {
- /**
- * @param callBack
- * 回调接口
- */
- public void executeCallBackOne(final ICallBackOne callBack) {
- System.out.println("**** 回调 ICallBackOne 开始...****");
- callBack.execute();
- System.out.println("*****回调 ICallBackOne 结束。*****");
- }
- /**
- * @param callBack
- * 回调接口
- * @param name
- * name
- * @param age
- * age
- */
- public void executeCallBackTwo(final ICallBackTwo callBack,
- final String name, final int age) {
- System.out.println();
- System.out.println("###### 回调 ICallBackTwo 开始...######");
- callBack.execute(name, age);
- System.out.println("#######回调 ICallBackTwo 结束。#######");
- }
- }
- /**
- * 测试类
- *
- * @author 张国明 guomingzhang2008@gmail.com <br/>
- * 2012-8-19 下午10:14:01
- *
- */
- public class Test {
- /**
- * @param args
- * arg
- */
- public static void main(final String[] args) {
- final CallBackImpl callBackMethod = new CallBackImpl();
- final ICallBackOne callBackOne = new ICallBackOne() {
- @Override
- public void execute() {
- System.out.println("调用 回调接口one中的方法");
- }
- };
- callBackMethod.executeCallBackOne(callBackOne);
- final ICallBackTwo callBackTwo = new ICallBackTwo() {
- @Override
- public void execute(final String name, final int age) {
- System.out.println("调用 回调接口 Two 中的方法\t" + name + " 的年龄是: "
- + age);
- }
- };
- callBackMethod.executeCallBackTwo(callBackTwo, "张国明", 28);
- }
- }
输出结果:
**** 回调 ICallBackOne 开始...****
调用 回调接口one中的方法
*****回调 ICallBackOne 结束。*****
###### 回调 ICallBackTwo 开始...######
调用 回调接口 Two 中的方法 张国明 的年龄是: 28
#######回调 ICallBackTwo 结束。#######
浅析Java抽象类和接口的比较
2007-11-26 11:11 作者:baocl 来源:赛迪网
[摘要] abstract class和interface是Java语言中对于抽象类定义进行支持的两种机制,正是由于这两种机制的存在,才赋予了Java强大的面向对象能力。
[关键字] Java 抽象 接口
[关键字] Java 抽象 接口
- Java接口 的理解和使用方法
- 对java 接口和实现的理解
- JAVA抽象类和接口的理解
- 理解Java的接口和抽象类
- java 抽象类和接口的理解
- java抽象类和接口的理解
- 针对中科院java接口的使用方法和问题
- java接口的理解
- Java接口的理解
- java接口的理解
- Java接口的理解
- Java的接口理解
- ThreadLocal的理解和使用方法
- AsyncTask的使用方法和理解
- AsyncTask的使用方法和理解
- AsyncTask的使用方法和理解
- AsyncTask的使用方法和理解
- AsyncTask的使用方法和理解
- 谈谈C语言中函数定义/声明冲突时的处理
- mysql 错误日志
- Matlab 生成可执行文件
- SQL 、Array.indexOf、Linq和DataTable.Select的效率对比
- C - Line
- Java接口 的理解和使用方法
- 基于C51的DMX512开发笔记
- 在“下一代网络”热潮中,中国 SDN(软件定义网络)会怎么走?
- 关于注册表问题
- 数据库模式
- 在ArcCatalog中和AE操作中是如何取消注册版本的问题
- tomcat的URIEncoding的作用
- in band & out of band
- Delphi学习笔记五——方法