View on GitHub

我的学习笔记

好记性不如烂笔头

8.5 泛型代码和虚拟机

目录

8.5.1 类型擦除

//Pair<T>的原始类型
//因为T是一个无限定的对象,所以直接用Object替换
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 setFisrt(Object newValue){ this.first=newValues;}
    public void setSecond(Object newValue){ this.second=newValues;}
}
//泛型类
public class Interval<T extends Comparable & Serializable> implements Serializable{
    private T lower;
    private T upper;
    //........
    public Interval(T first,T second){
        if(first.compareTo(second)<=0) {
            lower=first;
            upper=second;
        }
        else{
            lower=second;
            upper=first;
        }
    }
}

8.5.2 翻译泛型表达式

8.5.3 翻译泛型方法

8.5.4 调用遗留代码

返回