标签:java 泛型 javase java 面向对象 泛型设计
public class Couple {
private Object wife ;
private Object husband ;
public Couple(Object wife, Object husband) {
this.wife = wife;
this.husband = husband;
}
public void setWife(Object wife) {this. wife = wife;}
public void setHusband(Object husband) {this. husband = husband;}
public Object getWife() {return wife;}
public Object getHusband() {return husband;}
} public class Period<T extends Comparable<T> & Serializable> {
private T begin;
private T end;
public Period(T one, T two) {
if (one.compareTo(two) > 0) {begin = two;end = one;
} else {begin = one;end = two;}
}
} public class Period {
private Comparable begin;
private Comparable end;
public Period(Comparable one, Comparable two) {
if (one.compareTo(two) > 0) {begin = two; end = one;
} else {begin = one; end = two;}
}
} Couple<Employee> couple = ...;
Employee wife = couple.getWife();public static <T extends Comparable<T>> max(T[] arrays) {... }
擦除后成了:
public staticComoparable max(Comparable[] arrays) {... } public class Period <T extends Comparable<T> & Serializable> {
private T begin;
private T end;
public Period(T one, T two) {
if (one.compareTo(two) > 0) {begin = two;end = one;
} else {begin = one;end = two;}
}
public void setBegin(T begin) {this. begin = begin;}
public void setEnd(T end) {this. end = end;}
public T getBegin() {return begin;}
public T getEnd() {return end;}
}
public class DateInterval extends Period<Date> {
public DateInterval(Date one, Date two) {
super(one, two);
}
public void setBegin(Date begin) {
super.setBegin(begin);
}
} Period<Date> period = new DateInterval(...);
period.setBegin(new Date()); public void setBegin(Object begin) {
setBegin((Date)begin);
} @Override
public Date getBegin(){ return super.getBegin(); }标签:java 泛型 javase java 面向对象 泛型设计
原文地址:http://blog.csdn.net/ysjian_pingcx/article/details/40072247