调用方法报错ambiguous

代码报错信息

The method test(A1) is ambiguous for the type AmbiguousTest

 

源代码

class A1 {
}
interface B1 {
}
class C1 extends A1 implements B1 {
}

public class AmbiguousTest {
    void test(A1 a) {
    }
    void test(B1 b) {
    }

    void mytest() {
        C1 c1 = new C1();
        // test(c1);// error
    }
}


要调用一个方法,需要方法名+方法参数【类型,顺序,个数】用以定位该方法

此处调用方法名已经确定为test ,

而参数c1可以表示类型A1 也可以表示类型B1

ambiguous表示该调用有点歧义,导致系统不知道你想调用哪个方法。

 

解决方案 :将c1强转为A1或B1

void mytest() {
    C1 c1 = new C1();
    A1 a1 = (A1) c1;
    test(a1);
}





原文地址:https://www.cnblogs.com/fluffy/p/7003509.html