zoukankan      html  css  js  c++  java
  • 泛型操作实例

    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 

  • 相关阅读:
    Rancher之Pipeline JAVA demo
    rancher使用fluentd-pilot收集日志分享
    SFTP 命令列表以备查询
    WPF中异步更新UI元素
    Android 开发环境在 Windows7 下的部署安装
    Android Studio vs. Eclipse ADT Comparison
    JQuery 滚动条插件perfect-scrollbar
    nginx+php 在windows下的简单配置安装
    MySql 管理操作常用命令
    JS事件调试
  • 原文地址:https://www.cnblogs.com/penggy/p/7475860.html
Copyright © 2011-2022 走看看