import java.util.*;
class Generc_Text {
public static void main(String[] args){
Utils<Person> u = new Utils<Person>();
Person p = new Person("张三",23);
u.println(p);
u.show(p);
//u.println(new Student("李四",24));
//编译时不为指定Person将出现classCastException
u.show(new Student("王五",25));
}
}
class Utils<T>{
public void println(T t){
System.out.println(t);
}
public <Y> void show(Y y){
System.out.println(y);
}
}
class Person {
private String name;
private int age;
Person(String name,int age){
this.name = name;
this.age = age;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
}
class Student {
private String name;
private int age;
Student(String name,int age){
this.name = name;
this.age = age;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
}
|