![]() |
#2
hhwz2015-06-18 09:53
|

class test
{
static void f (float a)
{
System.out.println("float");
}
static void f (Character a)
{
System.out.println("Character");
}
}
public class Untitled
{
public static void main(String[] args)
{
test.f ('a') ;
}
}
{
static void f (float a)
{
System.out.println("float");
}
static void f (Character a)
{
System.out.println("Character");
}
}
public class Untitled
{
public static void main(String[] args)
{
test.f ('a') ;
}
}
如上的代码,编译没有问题,执行结果为:
float
//也就是说,编译器认为'a'更适合float
而如下代码:

class test
{
static void f (float... a)
{
System.out.println("float");
}
static void f (Character... a)
{
System.out.println("Character");
}
}
public class Untitled
{
public static void main(String[] args)
{
test.f ('a') ;
}
}
{
static void f (float... a)
{
System.out.println("float");
}
static void f (Character... a)
{
System.out.println("Character");
}
}
public class Untitled
{
public static void main(String[] args)
{
test.f ('a') ;
}
}
则提示编译错误,说参数'a'不明确,也就是说编译器分不清改传给哪个方法,但是上面的代码表示编译器认为比起Character,参数'a'更适合float,但是为什么到了可变参数的重载就说传参模糊,不知道该传给谁了呢?