- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是一个代码片段。
class BoxVar{
static void call(Integer... i){
System.out.println("hi"+i);
}
static void call(int... i ){
System.out.println("hello"+i);
}
public static void main(String... args){
call(10);
}
}
程序编译正常。当我运行程序时,它给了我
java: reference to call is ambiguous, both method call(java.lang.Integer...) in com.exams.BoxVar and method call(int...) in com.exams.BoxVar match
有人能解释一下原因吗?
最重要的是,我尝试将第一种方法转换为类似这样的方法。
static void call(Long... i){
System.out.println("hi"+i);
}
运行良好。有人可以帮我解决这个问题吗?谢谢。
最佳答案
编译器准确地告诉你发生了什么。 Integer
和 int
类型在概念上是相同的。因此,这两个方法签名是相同的。
假设编译成功,编译器应该调用哪个方法?
类型 Long
和 Integer
不一样(换句话说,编译器将整数类型视为两种不同的类型),这就是它编译成功的原因:
int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.
long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.
参见 Integral Types作为引用。
关于java - 为什么编译器会给出模棱两可的方法调用错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26577530/
我从一所大学获得了一些示例代码,导入了项目并尝试运行测试:方法 assertThat(Integer, Matcher) 对于 MyClass 类型是不明确的 每个 assertThat 都被标记为红
关于将 iOS 应用程序迁移到 Swift 3.0 的过程。这是我面临的一个问题。 先上相关代码: let calendar = NSCalendar.current, calendCompo = c
我刚开始研究 Java 8 Lambda 功能。我在 Java 7 中编写了这段代码,并尝试在 lamdas 中执行它。请注意,最后一行会产生编译错误,因为重载的函数不明确。我明白原因。如何使用 la
如何优先(告诉编译器)使用“函数接收引用”(#B)而不是“函数接收值”(#A)? #include using namespace std; class Heavy{/* ...... */}; /
我正在使用 Google Map API V3 显示车辆行驶路径及其路线方向。但是通过谷歌方向图标,很难找到方向。下图解释更多 我看到了每个图标,它是 source 我找到了图片路径,是 http:/
我是一名优秀的程序员,十分优秀!