Mic*_*ael 41
当您尝试实例化可以应用于多个构造函数的类时,存在问题.
例如:
public Example(String name) {
this.name = name;
}
public Example(SomeOther other) {
this.other = other;
}
Run Code Online (Sandbox Code Playgroud)
如果使用String对象调用构造函数,则有一个明确的构造函数.但是,如果你实例化new Example(null)
它可以适用于任何一个,因此是不明确的.
这同样适用于具有类似签名的方法.
这意味着您有两个具有相同签名的构造函数,或者您正在尝试Case
使用可以匹配多个构造函数的参数创建新实例.
在你的情况下:
Case(Problem, Solution, double, CaseSource)
Run Code Online (Sandbox Code Playgroud)
Java使用参数类型创建方法(构造函数)签名.您可以使用两种方法相同 类似的参数类型,因此可能通过提供可以匹配多个方法(构造函数)签名的模糊参数来生成模糊调用.
您可以使用以下代码重现此错误(这不是eclipse的错误):
class A {
public A(String a) { }
public A(Integer a) { }
static public void main(String...args) {
new A(null); // <== constructor is ambiguous
}
}
Run Code Online (Sandbox Code Playgroud)
要添加其他答案,可以通过将参数强制转换为预期来避免,例如:
class Foo {
public Foo(String bar) {}
public Foo(Integer bar) {}
public static void main(String[] args) {
new Foo((String) null);
}
}
Run Code Online (Sandbox Code Playgroud)