在java中,我正面臨函數模糊。基本上我重載一個可變參數函數
我確定我喜歡java中的函數模糊
static void f(Integer... a)
{
// .. some statements
}
static void f(float f,Integer... a)
{
// .. some other statements
}
函數可以調用函數與下面的函數調用
f(1,2);
f(1.2f,1,2);
此錯誤信息會彈出
error: reference to f is ambiguous, both method f(Integer...) in Test and method f(float,Integer...) in Test match
f(1,2);
^
有人可以幫助我瞭解,如果我在這裏錯過了任何基本的java概念。 thnx ..
但是如果我將第一個參數強制轉換爲float,f(float f,Integer ... a)版本將始終工作。我的意思是我似乎沒有找到一種方法來調用f(Integer ... a)版本。任何幫助? – Shashi
我不是在暗示,但是你可以使用函數f(char,Integer ... a)來繞過這個問題。沒有其他辦法,因爲一個整數總是可以作爲一個浮點數,這意味着你將永遠不明確... –
thnx指針。使用f(int b,Integer ... a)來解決歧義.. :) – Shashi