泛型操作实例

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 "联系方式" + "\n"
     + "\t地址:" + this.address + "\n"
     + "\t电话:" + this.telphone + "\n"
     + "\t邮编:" + this.zipcode + "\n";
     
 }
}
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 "基本信息" + "\n"
     + "\t姓名:" + this.name + "\n"
     + "\t性别:" + this.sex + "\n"
     + "\t年龄:" + this.age + "\n";
     
 }
}
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 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值