〖泛型〗_執行個體講解—泛型操作範例筆記

來源:互聯網
上載者:User

〖泛型〗_執行個體講解—泛型操作範例筆記

本章目標:
加深泛型的理解
掌握標識介面的定義

題目要求:
一個人中可以定義一個資訊的屬性,但是一個人可能有各種各樣的資訊(例如:連絡方式、基本資料等),所以此資訊屬性的類型就可以通過泛型進行聲明,之後只要設計相應的資訊類即可。

先設計人的資訊介面

interface Info{        // 只有此介面的子類才是表示人的資訊}class Contact implements Info{    // 表示連絡方式    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 "連絡方式:" + "\n" +                "\t|- 聯絡電話:" + this.telphone + "\n" +                "\t|- 聯絡地址:" + this.address + "\n" +                "\t|- 郵遞區號:" + this.zipcode ;    }};class Introduction implements Info{    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 "基本資料:" + "\n" +                "\t|- 姓名:" + this.name + "\n" +                "\t|- 性別:" + this.sex + "\n" +                "\t|- 年齡:" + this.age ;    }};class Person<T extends Info>{    private T info ;    public Person(T info){        // 通過構造方法設定資訊屬性內容        this.setInfo(info);    }    public void setInfo(T info){        this.info = info ;    }    public T getInfo(){        return this.info ;    }    public String toString(){    // 覆寫Object類中的toString()方法        return this.info.toString() ;    }};public class GenericsDemo32{    public static void main(String args[]){        Person<Contact> per = null ;        // 聲明Person對象        per = new Person<Contact>(new Contact("北京市","01051283346","100088")) ;        System.out.println(per) ;    }};

或者是:

interface Info{        // 只有此介面的子類才是表示人的資訊}class Contact implements Info{    // 表示連絡方式    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 "連絡方式:" + "\n" +                "\t|- 聯絡電話:" + this.telphone + "\n" +                "\t|- 聯絡地址:" + this.address + "\n" +                "\t|- 郵遞區號:" + this.zipcode ;    }};class Introduction implements Info{    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 "基本資料:" + "\n" +                "\t|- 姓名:" + this.name + "\n" +                "\t|- 性別:" + this.sex + "\n" +                "\t|- 年齡:" + this.age ;    }};class Person<T extends Info>{    private T info ;    public Person(T info){        // 通過構造方法設定資訊屬性內容        this.setInfo(info);    }    public void setInfo(T info){        this.info = info ;    }    public T getInfo(){        return this.info ;    }    public String toString(){    // 覆寫Object類中的toString()方法        return this.info.toString() ;    }};public class GenericsDemo33{    public static void main(String args[]){        Person<Introduction> per = null ;        // 聲明Person對象        per = new Person<Introduction>(new Introduction("李興華","男",30)) ;        System.out.println(per) ;    }};

OK,泛型內容整理完畢!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.