为什么带签名(primitive,wrapper)和(primitive,primitive)的两个方法会导致方法调用(包装器,原语)不明确? [英] Why do two methods with signature (primitive, wrapper) and (primitive, primitive) cause the method call (wrapper, primitive) to be ambiguous?

查看:149
本文介绍了为什么带签名(primitive,wrapper)和(primitive,primitive)的两个方法会导致方法调用(包装器,原语)不明确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是一个练习,但我无法弄清楚这种歧义:

It's just an exercise but I can't figure out the ambiguity:

private static void flipFlop(String str, int i, Integer iRef) {
System.out.println(str + "ciao");
}

private static void flipFlop(String str, int i, int j) {
System.out.println(str + "hello");
}

public static void main(String[] args) {
flipFlop("hello", new Integer(4), 2004);
}

它说:

方法flipFlop(String,int,Integer)对于类型
测试不明确

The method flipFlop(String, int, Integer) is ambiguous for the type Test

我猜想第二个参数将被解包到 int ,所以第二个 flipFlop 方法就是选择。

I would have guessed that the second argument would have been unwrapped to int and so the second flipFlop method would have been the choice.

推荐答案

如果你喜欢读书,这里是Java语言规范的相关部分,描述了如何解决方法。

If you enjoy riveting reading, here is the relevant portion of the Java Language specification that describes how methods are resolved.

但基本上你的第三个参数可以被解释为原始包装或自动装箱包装,编译器无法弄清楚你想要什么。两种方法都是 maximally specific 以使用JLS术语。

But basically your third argument can be interpreted as a primitive or an autoboxed wrapper, and the compiler can't figure out what you want. Both methods are "maximally specific" to use the JLS terminology.

这篇关于为什么带签名(primitive,wrapper)和(primitive,primitive)的两个方法会导致方法调用(包装器,原语)不明确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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