对于一个需要实现这样的功能的一个类:
Public class Count(){
private int count;
public void setCount(int count){
this.count = count;
}
public void getCount(){
return count;
}
}
对于不同类型的对象需重复地写这个类,或者使用泛型:
Public class Count<Type>{
private Type count;
public void setCount(int count){
this.count = count;
}
public void getCount(){
return count;
}
}
这样,对于不同类型的对象只需使用
Count<Integer> count = new Count<Integer>;
或
Count<String> count = new Count<String>;