(实际上Comparable也是一个泛型接口,所以最正确的定义方法是public static <T extends Comparable<? super T>> T min(T[] a) )
public class Pair<T> { private T first; private T second; public Pair(T first, T second) { this.first = first; this.second = second; } public T getFirst() { return first; } public T getSecond() { return second; } public void setFirst(T newValue) { first = newValue; } public void setSecond(T newValue) { second = newValue; } }Pair<T>类执行类型擦除后的原始类型就是
public class Pair { private Object first; private Object second; public Pair(Object first, Object second) { this.first = first; this.second = second; } public Object getFirst() { return first; } public Object getSecond() { return second; } public void setFirst(Object newValue) { first = newValue; } public void setSecond(Object newValue) { second = newValue; } }
class DateInterval extends Pair<Date> { public DateInterval(Date first, Date second) { super(first, second); } public void setSecond(Date second) { if (second.compareTo(getFirst()) >= 0) { super.setSecond(second); } else { throw new IllegalArgumentException("Second date should be no earlier than first date."); } } public Date getSecond() { return (Date) super.getSecond().clone(); } }
可以使用Jad工具来反编译class文件,就能清除到看到编译器所做的工作。
例如运行jad.exe DateInterval.class,生成下面的反编译文件
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov. // Jad home page: http://www.kpdus.com/jad.html // Decompiler options: packimports(3) // Source File Name: Pair.java package learning.generic; import java.util.Date; // Referenced classes of package learning.generic: // Pair class DateInterval extends Pair { public DateInterval(Date first, Date second) { super(first, second); } public void setSecond(Date second) { if(second.compareTo((Date)getFirst()) >= 0) super.setSecond(second); else throw new IllegalArgumentException("Second date should be no earlier than first date."); } public Date getSecond() { return (Date)((Date)super.getSecond()).clone(); } public volatile void setSecond(Object obj) { setSecond((Date)obj); } public volatile Object getSecond() { return getSecond(); } }
原文地址:http://blog.csdn.net/fw0124/article/details/42295463