public class Cat {
@Override
public String toString() {
return "一只小猫";//重写toString方法
@Override
public String toString() {
return "一只小猫";//重写toString方法
}
public class Dog {
@Override
public String toString() {
return "一只小狗";//重写toString方法
}
}
public class ShowObject<E> {
public void ShowMess(E e){
System.out.println(e.toString());//泛型类调用toString方法
}
}
public static void main(String[] args) {
ShowObject<Cat> showCat=new ShowObject<>();//声明泛型类对应showCat
ShowObject<Dog> showDog=new ShowObject<>();//声明泛型类对应showDog
showCat.ShowMess(new Cat());//showCat调用泛型类的ShowMess方法,并具体化Cat类
showDog.ShowMess(new Dog());//showDog调用泛型类的ShowMess方法,并具体化Dog类
}