码迷,mamicode.com
首页 > 其他好文 > 详细

泛型的练习

时间:2017-06-02 13:27:08      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:extends   back   err   end   类型   使用   定义   name   ring   

  用户在设计类的时候往往会使用类的关联关系,例如,一个人中可以定义一个信息属性,但是一个人可能有各种各样的信息(如联系方式基本信息等),所以

此信息属性的类型就可以通过泛型进行声明,然后只要设计相应的信息类即可。

 

  简单的分析: 一个人Person类,含有一个信息属性,但是信息的类型却有两种。产生了不确定性,所以采用泛型。

  解决之道:将Person类编写成自定义泛型的类,在实例化某个具体的person对象的时候,才去指定唯一信息的具体类型。

 

  1 package com.kj;
  2 
  3 public class test {
  4     
  5     public static void main(String[] args) {
  6         Person<Introduction> p =null ;
  7         p=new Person<Introduction>(new Introduction("李雷","男",24));
  8         System.out.println(p);
  9         Person<contact> p2 =null; 
 10         p2=new Person<contact>(new contact("北京市","01088888888","102206"));
 11         System.out.println(p2);
 12     }
 13 }
 14 class Person<T extends Info> {
 15     private T info;
 16     public Person(T info) {
 17         super();
 18         this.info = info;
 19     }
 20     public T getInfo() {
 21         return info;
 22     }
 23     public void setInfo(T info) {
 24         this.info = info;
 25     }
 26     @Override
 27     public String toString() {
 28         return "Person [info=" + info + "]";
 29     }
 30 }
 31 /*
 32  *  只有实现此接口的子类才是表示人的信息
 33  */
 34 interface Info{
 35 }
 36 /**
 37  * 代表着联系方式
 38  *
 39  */
 40 class contact implements Info{
 41     private String address ;    // 联系地址
 42     private String telephone ;    // 联系方式
 43     private String zipcode ;    // 邮政编码
 44     public String getAddress() {
 45         return address;
 46     }
 47     public void setAddress(String address) {
 48         this.address = address;
 49     }
 50     public String getTelephone() {
 51         return telephone;
 52     }
 53     public void setTelephone(String telephone) {
 54         this.telephone = telephone;
 55     }
 56     public String getZipcode() {
 57         return zipcode;
 58     }
 59     public void setZipcode(String zipcode) {
 60         this.zipcode = zipcode;
 61     }
 62     public contact(String address, String telephone, String zipcode) {
 63         super();
 64         this.address = address;
 65         this.telephone = telephone;
 66         this.zipcode = zipcode;
 67     }
 68     @Override
 69     public String toString() {
 70         return "contact [address=" + address + ", " +
 71                 "telephone=" + telephone
 72                 + ", zipcode=" + zipcode + "]";
 73     }
 74     
 75     
 76 }
 77 /**
 78  * 代表着基本信息
 79  */
 80 class Introduction implements Info{
 81     private String name ;        // 姓名
 82     private String sex ;        // 性别
 83     private int age ;            // 年龄
 84     
 85     public Introduction(String name, String sex, int age) {
 86         super();
 87         this.name = name;
 88         this.sex = sex;
 89         this.age = age;
 90     }
 91     public String getName() {
 92         return name;
 93     }
 94     public void setName(String name) {
 95         this.name = name;
 96     }
 97     public String getSex() {
 98         return sex;
 99     }
100     public void setSex(String sex) {
101         this.sex = sex;
102     }
103     public int getAge() {
104         return age;
105     }
106     public void setAge(int age) {
107         this.age = age;
108     }
109     @Override
110     public String toString() {
111         return "Introduction [name=" + name + ", sex="
112                 + sex + ", age=" + age
113                 + "]";
114     }
115     
116 }

 

  

 

泛型的练习

标签:extends   back   err   end   类型   使用   定义   name   ring   

原文地址:http://www.cnblogs.com/KEJUN/p/6932677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!