相关疑难解决方法(0)

varargs和重载的错误?

Java varargs实现中似乎存在一个错误.当方法使用不同类型的vararg参数重载时,Java无法区分适当的类型.

它给了我一个错误 The method ... is ambiguous for the type ...

请考虑以下代码:

public class Test
{
    public static void main(String[] args) throws Throwable
    {
        doit(new int[]{1, 2}); // <- no problem
        doit(new double[]{1.2, 2.2}); // <- no problem
        doit(1.2f, 2.2f); // <- no problem
        doit(1.2d, 2.2d); // <- no problem
        doit(1, 2); // <- The method doit(double[]) is ambiguous for the type Test
    }

    public static void doit(double... ds)
    {
        System.out.println("doubles");
    }

    public static void doit(int... is)
    {
        System.out.println("ints"); …
Run Code Online (Sandbox Code Playgroud)

java overloading variadic-functions

21
推荐指数
2
解决办法
3856
查看次数

标签 统计

java ×1

overloading ×1

variadic-functions ×1