我想知道是什麼Eclipse中的錯誤消息意味着:「構造函數......含糊不清」是什麼意思?
The constructor Case(Problem, Solution, double, CaseSource) is ambiguous
我想知道是什麼Eclipse中的錯誤消息意味着:「構造函數......含糊不清」是什麼意思?
The constructor Case(Problem, Solution, double, CaseSource) is ambiguous
的問題。
例如:
public Example(String name) {
this.name = name;
}
public Example(SomeOther other) {
this.other = other;
}
如果調用構造函數和String對象,有一個明確的構造函數。但是,如果您實例化new Example(null)
,那麼它可能適用於任何一種,因此不明確。
這同樣適用於具有相似簽名的方法。
這意味着你有兩個構造函數具有相同簽名,或者你想創建的Case
新實例參數可以匹配多個構造函數。
你的情況:
Case(Problem, Solution, double, CaseSource)
Java中創建方法(構造函數)的簽名與參數類型。您可以使用兩種類似的參數類型,因此可能通過提供可能匹配多個方法(構造函數)簽名的模糊參數來生成模糊調用。
您可以複製此錯誤與此代碼(這是不偏食的錯):當你嘗試實例可以適用於多個構造函數的類存在
class A {
public A(String a) { }
public A(Integer a) { }
static public void main(String...args) {
new A(null); // <== constructor is ambiguous
}
}
你怎麼能有兩個具有相同簽名的構造函數。在eclipse中它會給出錯誤:'類型Cls' – 2011-04-12 14:01:12
中的重複方法方法(params)不能有兩個具有相同簽名的構造函數(方法)。 – GuruKulki 2011-04-12 14:03:23
換句話說,不清楚哪個構造函數必須被調用。
它是一個評論而不是答案 – 2012-08-24 04:37:54
這是一個答案,但不是一個有用的答案。也許更長的解釋會有所幫助。 – Daniel 2015-06-26 22:08:43
要添加到其他的答案,它可以通過鑄造參數何意避免,例如:
class Foo {
public Foo(String bar) {}
public Foo(Integer bar) {}
public static void main(String[] args) {
new Foo((String) null);
}
}
你有沒有在'Case'類其他什麼構造? – biziclop 2011-04-12 13:53:55
而代碼是? – khachik 2011-04-12 13:54:29