在将重载与类型提升配合使用时,为什么方法调用是模棱两可的? [英] When using overloading with type promotion, why method calling is ambiguous?

查看:41
本文介绍了在将重载与类型提升配合使用时,为什么方法调用是模棱两可的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class aman {
    void m(double a , int b, int c) {
        System.out.println("second");
    }
    void m(float a , int b, double c) {
        System.out.println("first");
    }
    public static void main(String[] args) {
        aman obj = new aman();
        obj.m(23, 12, 1);
    }
}

在这里,方法m()已重载,但我不明白为什么调用不明确,因为在第一种方法中只需要进行一次转换,而在第二种方法中,则需要进行两次转换.因此,绝对应该调用第一种方法.请说明为什么这种情况不会发生,或者我错过了一些规则.

Here, method m() has been overloaded but i am not understanding why the call is ambigous because in first method only 1 conversion needs to take place whereas in second method, two conversions are required. So, definitely first method should have been called. Please give the reason why this is not happening or if i am missing some rule.

推荐答案

JLS不会将2次转换和1次转换视为差异.它只会区分必须转换不需要转换.

The JLS will not consider 2 conversions and 1 conversion to be a difference. It will only distinguish between having-to-convert and not-having-to-convert.

由于这两种方法都必须转换,因此同样可行.

Since both methods have-to-convert, they are equally possible.

关于此主题,有

Related to this subject, there is my answer on a similar question (but not entirely the same).

这篇关于在将重载与类型提升配合使用时,为什么方法调用是模棱两可的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆