黑马程序员技术交流社区
标题:
关于泛型类作为泛型类参数,大神给看看这么理解对么
[打印本页]
作者:
beckhamgun
时间:
2016-6-30 22:47
标题:
关于泛型类作为泛型类参数,大神给看看这么理解对么
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;
}
}
今天自习到泛型,遇到泛型类作为泛型类参数,蒙蔽了,自己做了个注释,大神给看看这么理解对么.
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2