【发布时间】:2014-07-01 01:56:04
【问题描述】:
运行此代码时,我在已注释的行中遇到错误。所以请帮我解决这个问题,我们如何使用 java 泛型重载函数。
public class Main
{
public static void main(String... args)
{
Main.<Integer>print();
Main.<Short>print();// error
Main.<Byte>print();// error
Main.<Void>print();//error
}
public static <T extends Integer> int print()
{
System.out.println("here - Integer");
return 0;
}
public static <T extends Short> short print() //error
{
System.out.println("here - Short");
return 0;
}
public static <T extends Byte> byte print() //error
{
System.out.println("here - Byte");
return 0;
}
public static <T extends Void> void print() //error
{
System.out.println("here - Void");
}
}
【问题讨论】:
-
它还能编译吗?
-
这不是方法覆盖。
-
这是方法重载,是的
-
由于泛型 docs.oracle.com/javase/tutorial/java/generics/erasure.html 的类型擦除,这不应该工作,因为运行时的方法签名将是相同的。
-
该代码无法编译。一个类中可能没有两个具有相同名称和参数的方法。