class Animal<T extends Animal> {
private Cook<T> cook = new Cook<T>(); public void call(){
cook.call((T) this);
}
}public class Cook<T extends Animal>{
public void call(T target){
System.out.println(target.getClass);
}
}
筒子们,上面第一个类中,cook.call((T) this)会有unchecked警告,但是已经民却表明T是自己的子类了。为什么自己强转成子类,还会有类型检查的警告呢?感觉上这一行不会抛出运行时异常啊。
泛型应该用在包装类上面。class Animal {
private Cook cook = new Cook();
public void call(){
cook.call(this);
}
}
public class Cook{
public void call(Animal target){
System.out.println(target.getClass());
}
}