在Java和Scala,我將如何做到以下幾點:創建一個參數是一個變函數
我希望能夠給一個函數傳遞到一個函數作爲參數,其中函數參數有所不同。例如,這就是我鎖定在現在的Java:
public void doSomething(Object object, Action1<Object> function) {
function.call(object);
}
public void doOtherThing(Object obj) {
System.out.println(obj);
}
doSomething("hello", this::doOtherThing);
這就是我希望能夠做到:
public void doSomethingFancy(Object object, <Some Function Type Here> function) {
function.call(object);
}
public void doFancyThing(Object obj1, String str1, List list1) {
// do stuff
}
public void doFancyThing2(Object obj1, RandomObj rObj, Integer int1) {
// do stuff
}
...
doSomething("hello", this::doFancyThing);
doSomething("hello", this::doFancyThing2);
基本上我想要的輸入功能具有可變型的輸入參數。 ActionN將無法工作,因爲這將傳遞一個對象數組到我的doFancyThing
方法,並且顯然不能編譯。
製作自定義界面。 – shmosel
過載加載方法簽名?你有一組特定的輸入參數嗎? – drelliot
更新了問題 –