還可以參考一篇 :
http://blog.csdn.net/cauchyweierstrass/article/details/44593807
感謝
開(kāi)發(fā)Android已有兩年了,說(shuō)來(lái)慚愧,回調(diào)雖然隨處可見(jiàn),并且網(wǎng)上關(guān)于回調(diào)函數(shù)和接口回調(diào)的內(nèi)容很多,可一直沒(méi)弄明白,總結(jié)不明白的原因如下:
1、java的接口定義以及向上轉(zhuǎn)型是理解回調(diào)的基礎(chǔ);
使用接口的核心原因:為了能夠向上轉(zhuǎn)型為多個(gè)基類(lèi)型。即利用接口的多實(shí)現(xiàn),可向上轉(zhuǎn)型為多個(gè)接口基類(lèi)型。
2、匿名類(lèi)。
代碼隨處可見(jiàn)new SthInterface()注冊(cè)接口回調(diào)。
感謝csdn兩篇文章讓我徹底理解回調(diào)函數(shù):
其實(shí)我很愿意理解網(wǎng)上那個(gè)關(guān)于打電話需求幫助的回調(diào)函數(shù)例子
在此我也寫(xiě)了一個(gè)與此類(lèi)似的例子:
1、首先定義一個(gè)接口(即回調(diào)接口)(幫助接口,可以向張三需求幫助,也可以向李四需求幫助,具體需要什么幫助,后期綁定自己實(shí)現(xiàn)。)
?
| 123 | publicinterface HelperInterface { voidexecute();} |
?
| 1234567 | publicclass HelperZhangsan implementsHelperInterface{ @Override publicvoid execute() { System.out.PRintln(This is zhangsan_helper.You can also ask lisi_helper!!); }} |
?
| 1234567891011 | publicclass Ask { privateHelperInterface helperInterface; publicvoid setHelperInterface(HelperInterface helperInterface){ //注冊(cè) this.helperInterface = helperInterface; } publicvoid resultForAsk(){ helperInterface.execute(); }} |
?
| 1234567 | publicclass Test { publicstatic void main(String[] args) { Ask ask = newAsk(); ask.setHelperInterface(newHelperZhangsan()); ask.resultForAsk(); }} |
?
| 12345678910111213 | publicclass Test { publicstatic void main(String[] args) { Ask ask = newAsk(); ask.setHelperInterface(newHelperInterface() { @Override publicvoid execute() { System.out.println(hell dsc); } }); ask.resultForAsk(); }} |
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注