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 

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

  • 相关阅读:
    设计模式之-工厂方法模式
    设计模式之-简单工厂模式
    设计模式之-单例模式
    Ubuntu-18.04 下使用Nginx搭建高可用,高并发的asp.net core集群
    Ubuntu-18.04 下修改root用户密码,安装SSH服务,允许root用户远程登录,安装vsftp服务器
    ASP.NET Core 系列[1]:ASP.NET Core 初识
    .net core系列之《将.net core应用部署到Ubuntu》
    动态内存分配函数
    C++ sort()对结构体排序
    STL
  • 原文地址:https://www.cnblogs.com/penggy/p/4786505.html
Copyright © 2011-2022 走看看