〖泛型〗_实例讲解—泛型操作范例笔记

〖泛型〗_实例讲解—泛型操作范例笔记

本章目标:
加深泛型的理解
掌握标识接口的定义

题目要求:
一个人中可以定义一个信息的属性,但是一个人可能有各种各样的信息(例如:联系方式、基本信息等),所以此信息属性的类型就可以通过泛型进行声明,之后只要设计相应的信息类即可。

先设计人的信息接口


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,泛型内容整理完毕!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

e421083458

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值