Learn from teacher Wang Generics (10): Generics applications

Source: Internet
Author: User

Application of Generics
Keynote Teacher: Wang Shaohua QQ Group: 483773664


First, the demand

When designing a class, users tend to use the association of classes, for example, a person can define a property of information, but one may have a variety of information (such as contact, basic information, etc.), so the type of this information property can be declared by generics, and then as long as the appropriate information class is designed.

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/80/3B/wKioL1c786XBxvUmAAAxBTLUCT0317.jpg " data_ue_src= "E:\My knowledge\temp\7a4a9479-8859-4c1b-a80f-4c54f93e9b18.jpg" >

Second, reference code 1, interface: Info
12 public interfaceInfo{     // 只有此接口的子类才是表示人的信息}
2. Contact information:
123456789101112131415161718192021222324252627282930313233 public class Contact implements Info{   // 表示联系方式    private String address ;    // 联系地址    private String telephone ;  // 联系方式    private String zipcode ;    // 邮政编码    public Contact(String address,String telephone,String zipcode){        this.address = address;        this.telephone = telephone;        this.zipcode = zipcode;    }    public void setAddress(String address){        this.address = address ;    }    public void setTelephone(String telephone){        this.telephone = telephone ;    }    public void setZipcode(String zipcode){        this.zipcode = zipcode;    }    public String getAddress(){        return this.address ;    }    public String getTelephone(){        return this.telephone ;    }    public String getZipcode(){        return this.zipcode;    }    @Override    public String toString() {        return "Contact [address=" + address + ", telephone=" + telephone                + ", zipcode=" + zipcode + "]";    }}
3, basic information: Introduction
123456789101112131415161718192021222324252627282930313233 public class Introduction implements Info{    private String name ;       // 姓名    private String sex ;        // 性别    private int age ;           // 年龄    public Introduction(String name,String sex,int age){        this.name = name;        this.sex = sex;        this.age = 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 ;    } Learn from teacher Wang Generics (10): Generics applications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.