〖泛型〗_实例讲解—泛型操作范例笔记

〖泛型〗_实例讲解—泛型操作范例笔记

分类: Java
〖泛型〗_实例讲解—泛型操作范例笔记

本章目标:
加深泛型的理解
掌握标识接口的定义

题目要求:
一个人中可以定义一个信息的属性,但是一个人可能有各种各样的信息(例如:联系方式、基本信息等),所以此信息属性的类型就可以通过泛型进行声明,之后只要设计相应的信息类即可。

先设计人的信息接口


[java]  view plain copy print ?
  1. interface Info{        // 只有此接口的子类才是表示人的信息  
  2. }  
  3. class Contact implements Info{    // 表示联系方式  
  4.     private String address ;    // 联系地址  
  5.     private String telphone ;    // 联系方式  
  6.     private String zipcode ;    // 邮政编码  
  7.     public Contact(String address,String telphone,String zipcode){  
  8.         this.setAddress(address) ;  
  9.         this.setTelphone(telphone) ;  
  10.         this.setZipcode(zipcode) ;  
  11.     }  
  12.     public void setAddress(String address){  
  13.         this.address = address ;  
  14.     }  
  15.     public void setTelphone(String telphone){  
  16.         this.telphone = telphone ;  
  17.     }  
  18.     public void setZipcode(String zipcode){  
  19.         this.zipcode = zipcode ;  
  20.     }  
  21.     public String getAddress(){  
  22.         return this.address ;  
  23.     }  
  24.     public String getTelphone(){  
  25.         return this.telphone ;  
  26.     }  
  27.     public String getZipcode(){  
  28.         return this.zipcode ;  
  29.     }  
  30.     public String toString(){  
  31.         return "联系方式:" + "\n" +  
  32.                 "\t|- 联系电话:" + this.telphone + "\n" +  
  33.                 "\t|- 联系地址:" + this.address + "\n" +  
  34.                 "\t|- 邮政编码:" + this.zipcode ;  
  35.     }  
  36. };  
  37. class Introduction implements Info{  
  38.     private String name ;        // 姓名  
  39.     private String sex ;        // 性别  
  40.     private int age ;            // 年龄  
  41.     public Introduction(String name,String sex,int age){  
  42.         this.setName(name) ;  
  43.         this.setSex(sex) ;  
  44.         this.setAge(age) ;  
  45.     }  
  46.     public void setName(String name){  
  47.         this.name = name ;  
  48.     }  
  49.     public void setSex(String sex){  
  50.         this.sex = sex ;  
  51.     }  
  52.     public void setAge(int age){  
  53.         this.age = age ;  
  54.     }  
  55.     public String getName(){  
  56.         return this.name ;  
  57.     }  
  58.     public String getSex(){  
  59.         return this.sex ;  
  60.     }  
  61.     public int getAge(){  
  62.         return this.age ;  
  63.     }  
  64.     public String toString(){  
  65.         return "基本信息:" + "\n" +  
  66.                 "\t|- 姓名:" + this.name + "\n" +  
  67.                 "\t|- 性别:" + this.sex + "\n" +  
  68.                 "\t|- 年龄:" + this.age ;  
  69.     }  
  70. };  
  71. class Person<T extends Info>{  
  72.     private T info ;  
  73.     public Person(T info){        // 通过构造方法设置信息属性内容  
  74.         this.setInfo(info);  
  75.     }  
  76.     public void setInfo(T info){  
  77.         this.info = info ;  
  78.     }  
  79.     public T getInfo(){  
  80.         return this.info ;  
  81.     }  
  82.     public String toString(){    // 覆写Object类中的toString()方法  
  83.         return this.info.toString() ;  
  84.     }  
  85. };  
  86. public class GenericsDemo32{  
  87.     public static void main(String args[]){  
  88.         Person<Contact> per = null ;        // 声明Person对象  
  89.         per = new Person<Contact>(new Contact("北京市","01051283346","100088")) ;  
  90.         System.out.println(per) ;  
  91.     }  
  92. };  



或者是:

[java]  view plain copy print ?
  1. interface Info{        // 只有此接口的子类才是表示人的信息  
  2. }  
  3. class Contact implements Info{    // 表示联系方式  
  4.     private String address ;    // 联系地址  
  5.     private String telphone ;    // 联系方式  
  6.     private String zipcode ;    // 邮政编码  
  7.     public Contact(String address,String telphone,String zipcode){  
  8.         this.setAddress(address) ;  
  9.         this.setTelphone(telphone) ;  
  10.         this.setZipcode(zipcode) ;  
  11.     }  
  12.     public void setAddress(String address){  
  13.         this.address = address ;  
  14.     }  
  15.     public void setTelphone(String telphone){  
  16.         this.telphone = telphone ;  
  17.     }  
  18.     public void setZipcode(String zipcode){  
  19.         this.zipcode = zipcode ;  
  20.     }  
  21.     public String getAddress(){  
  22.         return this.address ;  
  23.     }  
  24.     public String getTelphone(){  
  25.         return this.telphone ;  
  26.     }  
  27.     public String getZipcode(){  
  28.         return this.zipcode ;  
  29.     }  
  30.     public String toString(){  
  31.         return "联系方式:" + "\n" +  
  32.                 "\t|- 联系电话:" + this.telphone + "\n" +  
  33.                 "\t|- 联系地址:" + this.address + "\n" +  
  34.                 "\t|- 邮政编码:" + this.zipcode ;  
  35.     }  
  36. };  
  37. class Introduction implements Info{  
  38.     private String name ;        // 姓名  
  39.     private String sex ;        // 性别  
  40.     private int age ;            // 年龄  
  41.     public Introduction(String name,String sex,int age){  
  42.         this.setName(name) ;  
  43.         this.setSex(sex) ;  
  44.         this.setAge(age) ;  
  45.     }  
  46.     public void setName(String name){  
  47.         this.name = name ;  
  48.     }  
  49.     public void setSex(String sex){  
  50.         this.sex = sex ;  
  51.     }  
  52.     public void setAge(int age){  
  53.         this.age = age ;  
  54.     }  
  55.     public String getName(){  
  56.         return this.name ;  
  57.     }  
  58.     public String getSex(){  
  59.         return this.sex ;  
  60.     }  
  61.     public int getAge(){  
  62.         return this.age ;  
  63.     }  
  64.     public String toString(){  
  65.         return "基本信息:" + "\n" +  
  66.                 "\t|- 姓名:" + this.name + "\n" +  
  67.                 "\t|- 性别:" + this.sex + "\n" +  
  68.                 "\t|- 年龄:" + this.age ;  
  69.     }  
  70. };  
  71. class Person<T extends Info>{  
  72.     private T info ;  
  73.     public Person(T info){        // 通过构造方法设置信息属性内容  
  74.         this.setInfo(info);  
  75.     }  
  76.     public void setInfo(T info){  
  77.         this.info = info ;  
  78.     }  
  79.     public T getInfo(){  
  80.         return this.info ;  
  81.     }  
  82.     public String toString(){    // 覆写Object类中的toString()方法  
  83.         return this.info.toString() ;  
  84.     }  
  85. };  
  86. public class GenericsDemo33{  
  87.     public static void main(String args[]){  
  88.         Person<Introduction> per = null ;        // 声明Person对象  
  89.         per = new Person<Introduction>(new Introduction("李兴华","男",30)) ;  
  90.         System.out.println(per) ;  
  91.     }  
  92. };  



OK,泛型内容整理完毕!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值