有沒有在java的方式做這樣的事情:參考fnc
void fnc(void Reference_to_other_func());
我想要的是基本上我有一些地方,我需要向用戶顯示相同的文本,唯一的區別是在此文本後調用哪個方法。因此,例如,而不是書寫:
System.out.println("Hello");
f1();
//in some other place
System.out.println("Hello");
f2();
//etc
我想定義一個函數:
public void f(void Reference_to_other_func())
{
System.out.println("Hello");
Reference_to_other_func();//HERE I'M INVOKING
}
,然後,而不是重複這整個代碼,我可以寫這樣的事:
f(f1);
//in some other place
f(f2)
//etc.
感謝您的回答
f1()和f2()必須在同一個類中嗎? – 2010-03-25 15:39:07
你爲什麼要這樣做?您可以直接傳遞對變量或對象的引用或調用方法。您也可以嘗試使用功能語言,您可以像Haskell一樣執行此操作。 – 2010-03-25 15:50:48