泛型范例

interface Info
{
}
class Person<T extends Info>
{
    private T information;
public Person(T information)
{
this.setInformation(information);
}
public void setInformation(T information)
{
this.information = information;
}
public T getInformation()
{
return this.information;
}
public String toString()
{
return this.information.toString();
}
}
class Wei implements Info
{
private String province;
    private String street;
private String zipcode;
public Wei(String province,String street,String zipcode)
{
this.setProvince(province);
this.setStreet(street);
this.setZipcode(zipcode);
}
public void setProvince(String province)
{
this.province = province;
}
public String getProvince()
{
return this.province;
}
public void setStreet(String street)
{
this.street = street;
}
public String getStreet()
{
return this.street;
}
public void setZipcode(String zipcode)
{
this.zipcode = zipcode;
}
public String getZipcode()
{
return this.zipcode;
}
public String toString()
{
return "city: " + getProvince() +
"street: " + getStreet() +
"zipcode: " + getZipcode();
}
}
class Int implements Info
{
private String name;
    private String sex;
private int age;
public Int(String name,String sex,int age)
{
this.setName(name);
this.setSex(sex);
this.setAge(age);
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setSex(String sex)
{
this.sex = sex;
}
public String getSex()
{
return this.sex;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return this.age;
}
public String toString()
{
return "name: " + getName() +
"sex: " + getSex() +
"age: " + getAge();
}                        
}
public class Demo110
{
public static void main(String args[])
{
Person<Wei> per = new Person<Wei>( new Wei("beijing","guangdong","45133"));
//Person<Int> per = new Person<Int>( new Int("JIM","男",23));
System.out.println(per);
}
}
posted @ 2014-10-20 21:50  awenzero  阅读(106)  评论(0编辑  收藏  举报