gpt4 book ai didi

java - 可变参数重载

转载 作者:行者123 更新时间:2023-12-04 04:45:09 33 4
gpt4 key购买 nike

任何人都可以解释为什么这段代码会抛出一个不明确的重载错误,整数方法肯定更具体和适用吗?

谢谢,

内德

package object_orientation;
public class Ambiguous {
//ambiguous error compiler unsure whether boxing is needed or not
static void overload(Integer... d){
System.out.println("Integer");
}

static void overload(long... d){
System.out.println("Long");
}

public static void main(String a[]){
int i = 1;
overload(i);
}
}

最佳答案

Java 中的这些概念应该会有所帮助,Boxing + Widening is allowed ,但是 not Widening + Boxing .

这些加宽、拳击和 Vararg 规则应该会有所帮助:

  1. Primitive Widening > Boxing > Varargs.
  2. Widening and Boxing (WB) not allowed.
  3. Boxing and Widening (BW) allowed.
  4. While overloading, Widening + vararg and Boxing + vararg can only be used in a mutually exclusive manner
  5. Widening between wrapper classes not allowed.
  6. Widening+varArgs & Boxing+varargs are individually allowed (but not allowed in overloaded version of method).
  7. Boxing+Widening is preferred over Boxing+Varargs.


希望这会有所帮助。

关于java - 可变参数重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18314454/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com