public class GenClassDemo {
public static void main(String[] args){
GenClass <GenClass <Student>> d = new GenClass <GenClass <Student>>();
GenClass <Student> e = new GenClass <Student>();
e.setData(new Student("Henry")); //即: Student data = new Student("Henry");
d.setData(e); //即: GenClass <Student> data = e;
System.out.println("实例d: " + d.getData().getData()/*.toStrng()*/);
// |(取出)
// (泛型实例)e .getData()
// |(取出)
// new Student("Henry")
}
}
class GenClass <T> {
private T data;
public GenClass() {
}
public GenClass(T data) {
this.data = data;
}
public void setData(T data) {
this.data = data;
}
public T getData() {
return data;
}
}
class Student {
private String name;
public Student(String name) {
this.name = name;
}
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "My name is : " + name;
}
}
今天自习到泛型,遇到泛型类作为泛型类参数,蒙蔽了,自己做了个注释,大神给看看这么理解对么. |
|