• 泛型操作实例


    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 

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    压测场景下的 TIME_WAIT 处理
    拥抱云原生,Fluid结合JindoFS :阿里云OSS加速利器
    从DHTML、HTC、XHTML到AJAX
    altas(ajax)控件(一):多功能面板控件Accordion
    fedora7 常用软件安装
    Fedora7安装后的配置
    .net程序员的盲点(六):StringBuilder 和 String 的区别
    .net程序员的盲点(五):告诉你一个不一样的new
    .net程序员的盲点(四):索引器Indexers
    员工究竟渴望学到的是什么?-(杂谈-20070816)
  • 原文地址:https://www.cnblogs.com/penggy/p/4786505.html
Copyright © 2020-2023  润新知