还可以参考一篇 :
http://blog.csdn.net/cauchyweierstrass/article/details/44593807
感谢
开发Android已有两年了,说来惭愧,回调虽然随处可见,并且网上关于回调函数和接口回调的内容很多,可一直没弄明白,总结不明白的原因如下:
1、java的接口定义以及向上转型是理解回调的基础;
使用接口的核心原因:为了能够向上转型为多个基类型。即利用接口的多实现,可向上转型为多个接口基类型。
2、匿名类。
代码随处可见new SthInterface()注册接口回调。
感谢csdn两篇文章让我彻底理解回调函数:
其实我很愿意理解网上那个关于打电话需求帮助的回调函数例子
在此我也写了一个与此类似的例子:
1、首先定义一个接口(即回调接口)(帮助接口,可以向张三需求帮助,也可以向李四需求帮助,具体需要什么帮助,后期绑定自己实现。)
?
123 | public interface HelperInterface { void execute(); } |
?
1234567 | public class HelperZhangsan implements HelperInterface{ @Override public void execute() { System.out.PRintln(This is zhangsan_helper.You can also ask lisi_helper!!); } } |
?
1234567891011 | public class Ask { private HelperInterface helperInterface; public void setHelperInterface(HelperInterface helperInterface){ //注册 this .helperInterface = helperInterface; } public void resultForAsk(){ helperInterface.execute(); } } |
?
1234567 | public class Test { public static void main(String[] args) { Ask ask = new Ask(); ask.setHelperInterface( new HelperZhangsan()); ask.resultForAsk(); } } |
?
12345678910111213 | public class Test { public static void main(String[] args) { Ask ask = new Ask(); ask.setHelperInterface( new HelperInterface() { @Override public void execute() { System.out.println(hell dsc); } }); ask.resultForAsk(); } } |
新闻热点
疑难解答