有沒有什麼辦法可以引用Java 8中的一組方法,這會改變它們的簽名方式?Java 8中的方法引用:是否支持重載?
更確切地說,我想這個代碼工作:
public class OverloadingMethodRef
{
public static void foo (int x) {
System.out.println ("An integer: " + x);
}
public static void foo (String x) {
System.out.println ("A String: " + x);
}
/**
* I want it to work without this
*/
// public static void foo (Object x) {
// if (x instanceof Integer) foo ((int) x); else foo ((String) x);
// }
public static void main (String[] args)
{
// Compile error, it wants a precise reference to one of them
Consumer<Object> c = PolymorphicMethodRef::foo;
foo ("bla"); // despite it could get it
foo (1); // and here too
}
}
我不能添加public static void foo (Object x)
,因爲我有很多的方法來傳遞給另一個方法,我不想寫包裝。到目前爲止,我只能通過反射(它可以接收param.getClass()
)來完成此操作,但我必須調用的方法具有不同的參數(> = 1),每次需要將它們放入數組中時,再將它們的類型放入另一箇中。
支持多態。也沒有 - 這不是多態,這是**重載**。不,沒有 - Java編程方法在編譯時調用特定的方法重載。 –
Whops!是的,我的意思是超載,我解決了這個問題,謝謝。 – zakmck
確實不可能獲得方法引用,就好像它指向重載方法一樣。你可以做的最好的方法是添加另一種方法,如void foo(Object x),並讓用戶退出,但它仍然會始終調用該方法(使用Object參數),永遠不會解析其他任何東西。 –