给你一个JAVA常用的回调方式,JAVA里面一般都是这样用的。//1.需要回调的类 public class A{ public int testCallback(ICallback ac){ ...... Object result=ac.invoke(..); ..... } } //2.回调接口 public interface ICallback{ Object invoke(...); }//3.实现回调接口 public class Callback implemnts ICallback{ ....... }public class Test{ public static void main(...){ A a=new A(); a.testCallback(new Callback ()); } }
一般用匿名内部类实现callback public interface YourInterface { public void interfaceMethod(); } public class YourClass{ public void classMethod(YourInterface yInterface) { ...... yInterface.interfaceMethod(); ...... }
public static void main(String[] args) { YourClass yClass = new YourClass(); yClass.classMethod(new YourInterface() { public void interfaceMethod() { //do sth System.out.print("hello world."); ....... } }); }
接口除了可以向上转型,也是一种协议,实现了这个接口,就必须遵循接口里面的方法签名。
给你一个JAVA常用的回调方式,JAVA里面一般都是这样用的。//1.需要回调的类
public class A{
public int testCallback(ICallback ac){
......
Object result=ac.invoke(..);
.....
}
}
//2.回调接口
public interface ICallback{
Object invoke(...);
}//3.实现回调接口
public class Callback implemnts ICallback{
.......
}public class Test{
public static void main(...){
A a=new A();
a.testCallback(new Callback ());
}
}
一般情况下,回调接口都是直接采用匿名类的方式来实现的,呵呵,在JAVA里实现回调就是这么麻烦,没有办法
public class Test{
public static void main(...){
A a=new A();
a.testCallback(new ICallback(){
……
Object invoke(...);
……
});
}
} PS.为什么我的FireFox无法使用“插入源代码”功能????!!!!
{
// 这仅是一个常规方法。因此如果需要,
// 它可有返回值,也可接收参数。
public void interestingEvent ();
}这使得我们可以控制实现该接口的类的任何对象。因此,我们不必关心任何外部类型信息。与在将 C++ 代码用于 Motif 时使用窗口小部件的数据域来容纳对象指针的难以控制的 C 函数相比,这种方法要好得多。发出事件信号的类必须等待实现了 InterestingEvent 接口的对象,并在适当时候调用 interestingEvent() 方法。public class EventNotifier
{
private InterestingEvent ie;
private boolean somethingHappened;public EventNotifier (InterestingEvent event)
{
// 保存事件对象以备后用。
ie = event;// 还没有要报告的事件。
somethingHappened = false;
}//...public void doWork ()
{
// 检查在别处设置的谓词。
if (somethingHappened)
{
// 通过调用接口的这个方法发出事件信号。
ie.interestingEvent ();
}
//...
}// ...
}在上例中,我使用 somethingHappened 谓词来跟踪是否应触发事件。在许多情况下,调用此方法足以保证向 interestingEvent() 发出信号。希望接收事件通知的代码必须实现 InterestingEvent 接口,并将自身引用传递给事件通知程序。public class CallMe implements InterestingEvent
{
private EventNotifier en;public CallMe ()
{
// 创建事件通知程序,并将自身引用传递给它。
en = new EventNotifier (this);
}// 为事件定义实际的处理程序。
public void interestingEvent ()
{
// 噢!必定发生了感兴趣的事件!
// 执行某些操作 ...
}//...
}
public class Test{
public static void main(...){
A a=new A();
a.testCallback(new ICallback(){
……
Object invoke(...){....};
……
});
}
}
如cloneable接口,任何实现cloneable接口的类都可以被克隆
上面两点结合向上转型技术,就说明通过接口实现多态性
另外:接口事实上充当一个和其他类交互的角色,其他人要跟你所设计的系统进行交互,可能只需要知道你的接口是怎样就行了,背后具体怎么设计不需要关心。从这方面讲,接口实现了封装性。当然,这也要求你所设计的接口要稳定,不能随便改变。如果一改变的话,其他跟你交互的系统也要修改。接口也相当于一个描述性的东西,告诉你,实现了这个接口的类到底能做些什么。接口有点类似于sun公司提出了jvm的specification,告诉大家虚拟机应该有些什么功能,但是具体怎么实现这个虚拟机就由各大公司实现,如IBM的jvm,MS的JVM,SUN的JVM,这些jvm看起来、用起来都差不多,因为他们只不过是实现了sun公司的jvm的specification.这个specification就有点像接口。扯了这么多,不知道lz看懂了没?
<td colspan="5">
<table width="97%" border="0" align="center">
<tr>
<td>
欢迎你访问论坛:<%=username%>
</td>
<td align="right">
<A href="<%=request.getContextPath()%>/backtoindex.do"><tr align="left">
<td colspan="5">
<table width="97%" border="0" align="center">
<tr>
<td>
欢迎你访问论坛:<%=username%>
</td>
<td align="right">
<A href="<%=request.getContextPath()%>/backtoindex.do">
首页
</A>
<A href="logoff.do">
<tr align="left">
<td colspan="5">
<table width="97%" border="0" align="center">
<tr>
<td>
欢迎你访问论坛:<%=username%>
</td>
<td align="right">
<A href="<%=request.getContextPath()%>/backtoindex.do">
首页
<tr align="left">
<td colspan="5">
<table width="97%" border="0" align="center">
<tr>
<td>
欢迎你访问论坛:<%=username%>
</td>
<td align="right">
<A href="<%=request.getContextPath()%>/backtoindex.do">
首页
</A>
<A href="logoff.do">
注销登录
</A>
<A href="<%=request.getContextPath()%>/newtopic.do">
发新话题
</A>
</td>
</tr>
</table>
</td>
</tr> </A>
<A href="logoff.do">
注销登录
</A>
<A href="<%=request.getContextPath()%>/newtopic.do">
发新话题
</A>
</td>
</tr>
</table>
</td>
</tr> 注销登录
</A>
<A href="<%=request.getContextPath()%>/newtopic.do">
发新话题
</A>
</td>
</tr>
</table>
</td>
</tr>
首页
</A>
<A href="logoff.do">
注销登录
</A>
<A href="<%=request.getContextPath()%>/newtopic.do">
发新话题
</A>
</td>
</tr>
</table>
</td>
</tr>kjkjkjk
public interface YourInterface { public void interfaceMethod(); }
public class YourClass{ public void classMethod(YourInterface yInterface)
{
......
yInterface.interfaceMethod();
......
}
public static void main(String[] args)
{
YourClass yClass = new YourClass(); yClass.classMethod(new YourInterface()
{
public void interfaceMethod()
{
//do sth
System.out.print("hello world.");
.......
}
});
}