可能重複:
What's the nearest substitute for a function pointer in Java?
Callback functions in JavaJava:我們如何在「禁止函數指針」時創建回調函數?
我想問術語回調的一些概念。
使用回調的主要目的是什麼?它只是爲了做一些異步功能嗎?從維基,我無法得到實際意義。
這部分代碼是從wiki- callback
void PrintTwoNumbers(int (*numberSource)(void)) {
printf("%d and %d\n", numberSource(), numberSource());
}
/* One possible callback. */
int overNineThousand(void) {
return (rand() % 1000) + 9000;
}
/* Here we call PrintTwoNumbers() with three different callbacks. */
int main(void) {
PrintTwoNumbers(overNineThousand);
}
從維基複製,它說我們需要傳遞一個函數指針作爲其他函數的參數爲了做一個回調。
但是在java中,當我們調用一個函數時,沒有辦法通過引用傳遞,我們可以像Java代碼那樣在Java中創建一個回調函數嗎?
感謝
類似:HTTP:// stackoverflow.com/questions/443708/callback-functions-in-java http://stackoverflow.com/questions/122407/whats-the-nearest-substitute-for-a-function-pointer-in-java –
什麼是使用回調的主要目的是什麼? [好萊塢原則](http://en.wikipedia.org/wiki/Hollywood_Principle)是一個很好的比喻! – home