What does “The constructor … is ambiguous” mean?

后端 未结 4 1006
花落未央
花落未央 2021-01-07 17:37

I would like to know what the error message in Eclipse means:

The constructor Case(Problem, Solution, double, CaseSource) is ambiguous

4条回答
  •  青春惊慌失措
    2021-01-07 18:12

    The problem exists when you try to instantiate a class that could apply to more than one constructor.

    For example:

    public Example(String name) {
        this.name = name;
    }
    
    public Example(SomeOther other) {
        this.other = other;
    } 
    

    If you call the constructor with a String object, there's one definite constructor. However, if you instantiate new Example(null) then it could apply to either and is therefore ambiguous.

    The same can apply to methods with similar signatures.

提交回复
热议问题