• 泛型操作实例


    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 

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

  • 相关阅读:
    镜像(Image)、容器(Container)
    奇安信处理多起服务器挖矿应急事件
    英特尔® Threat Detection Technology——这玩意检测无文件攻击、挖矿、勒索就是神器啊
    Necro挖矿使用了使用Tor+动态域名DGA
    “挖矿”恶意代码肆虐,安天智甲有效防护——属于异常检测
    深信服挖矿检测——EDR没有检测能力?
    活跃的H2Miner组织挖矿分析,含样本本地复现【EDR 经典case】
    CNCERT:2021年恶意挖矿威胁趋势分析报告
    vscode ${workspaceFolder} can not be resolved
    Streamlit 简介
  • 原文地址:https://www.cnblogs.com/penggy/p/4786505.html
Copyright © 2020-2023  润新知