方法重载编译错误,方法调用不明确

Method overloading compiler error, ambiguous method call

对于下面的代码,我确实理解调用方法不清楚要调用哪个重载方法是有道理的,但是我无法理解这里是如何检查参数匹配的。

public class Test{
    public void m1(float i,int j){
        System.out.println("float method ");
    }

    public void m1(int i, float j){
        System.out.println("int method ");
    }

    public static void main(String[] args) {
        Test a=new Test();
        a.m1(10,10); // The method m1(float, int) is ambiguous for the type Test
    }
}

使用时应使参数之一显式浮动,即使用 a.m1(10f, 10);a.m1(10, 10f); 否则编译器无法识别调用哪个方法

如果多个成员方法既可访问又适用于方法调用,Java 编程语言使用选择最具体方法的规则。所以这里 java 编译器不认为它们中的任何一个更具体,因此错误。

详细理解请阅读JLS