标签:java 泛型 javase java 面向对象 泛型设计
byte Byte 0
short Short 0
int Integer 0
long Long 0L
float FLoat 0.0F
double Double 0.0D
char Character '\u0000'
boolean Boolean false public Couple() {wife = new T(); husband = new T();} // 错误 public static <T> Couple<T> createInstance(Class<T> clazz) {
try {
return new Couple<T>(clazz.newInstance(), clazz.newInstance());
} catch (Exception e) {
return null ;
}
} public static <T> T[] maxTwo(T[] values) {T[] array = new T[2];} // 错误 public static <T extends Comparable<T>> T[] maxTwo(T[] array) {
Object[] result = new Object[2];
return (T[]) result; // Type safety: Unchecked cast from Object[] to T[]
} public static <T extends Comparable<T>> T[] maxTwo(T[] array) {
// Type safety: Unchecked cast from Object[] to T[]
return (T[]) Array.newInstance(array.getClass().getComponentType(), 2) ;
} if (arg1 instanceof Couple<Employee>){...} // 错误 public static <T extends Throwable> void doSomething(Class<T> t) {
try {
// do something...
} catch (T e) {
e.printStackTrace();
}
} // 错误 public static <T extends Throwable> void doSomething(T t) throws T {
try {
// do something...
} catch (Throwable e) {
e.printStackTrace();
throw t;
}
}// 正确public class Singleton <T>{
private static T instance;
public static T getInstance(){ ..}
} public class NameClash<T> {
public boolean equals(T value) {
return false ;
}
} class Calendar implements Comparable<Calendar> {...}
class GregorianCalendar extends Calendar implements Comparable<GregorianCalendar> {...}
标签:java 泛型 javase java 面向对象 泛型设计
原文地址:http://blog.csdn.net/ysjian_pingcx/article/details/40106363