interface InfoPerson{
}
class Contact implements InfoPerson{
private String address;
private String telphone;
private String zipcode;
public Contact (String address, String telphone, String zipcode){
this.setAddress(address);
this.setTelphone(telphone);
this.setZipcode(zipcode);
}
public void setAddress(String address){
this.address = address;
}
public void setTelphone(String telphone){
this.telphone = telphone;
}
public void setZipcode(String zipcode){
this.zipcode = zipcode;
}
public String getAddress(){
return this.address;
}
public String getTelphone(){
return this.telphone;
}
public String getZipcode(){
return this.zipcode;
}
public String toString(){
return "联系方式" + "
"
+ " 地址:" + this.address + "
"
+ " 电话:" + this.telphone + "
"
+ " 邮编:" + this.zipcode + "
";
}
}
class Introduction implements InfoPerson{
private String name;
private String sex;
private int age;
public Introduction(String name, String sex,int age){
this.setName(name);
this.setSex(sex);
this.setAge(age);
}
public void setName(String name){
this.name = name;
}
public void setSex(String sex){
this.sex = sex;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return this.name;
}
public String getSex(){
return this.sex;
}
public int getAge(){
return this.age;
}
public String toString(){
return "基本信息" + "
"
+ " 姓名:" + this.name + "
"
+ " 性别:" + this.sex + "
"
+ " 年龄:" + this.age + "
";
}
}
class Person <T extends InfoPerson>{
private T infoperson;
public Person(T infoperson){
this.setInfoPerson(infoperson);
}
public void setInfoPerson(T infoperson){
this.infoperson = infoperson;
}
public T getInfoPerson(){
return this.infoperson;
}
public String toString(){
return this.infoperson.toString();
}
}
public class GenericsDemo6 {
public static void main(String[] args) {
Person per = null;
Person per2 = null;
per = new Person<Contact>(new Contact("北京","13337105737","310000"));
per2 = new Person<Introduction>(new Introduction("张三","男",20));
System.out.println(per2);
System.out.println(per);
}
}
运行结果如下:
基本信息
姓名:张三
性别:男
年龄:20
联系方式
地址:北京
电话:13337105737
邮编:310000
版权声明:本文为博主原创文章,未经博主允许不得转载。