interface Info{
};
class Introduction implements Info{
String name;
String age;
String sex;
public Introduction(String name,String age,String sex){
this.name = name;
this.age = age;
this.sex = sex;
}
public String toString(){
return "个人信息:\n" +
"\t姓名:" + name +
"\t年龄:" + age +
"\t性别:" + sex;
}
};
class Person<T extends Info>{
T per;
public Person(T info){
this.per = info;
}
public String toString(){
return per.toString();
}
};
public class InfoAnd{
public static void main(String args[]){
Person<Introduction> per = null;
per = new Person<Introduction>(new Introduction("xx","30","girl"));
System.out.println(per.toString());
}
}就是为了熟悉泛型的操作
本文通过实例展示了如何在Java中使用泛型来创建一个可以存储任意类型信息的Person类,并通过实现Info接口来封装个人信息。文章详细介绍了如何定义泛型类、创建实例并调用toString方法来展示信息。

被折叠的 条评论
为什么被折叠?



