首页 > 其他 > 详细

自定义泛型类

时间:2021-09-06 06:33:51      阅读:16      评论:0      收藏:0      [点我收藏+]
package demo02;

/**
 * @description: demo05
 * @author: liuyang
 * @create: 2021-09-04 17:18
 */
public class Demo05 {
    public static void main(String[] args) {
        Order<Integer> order = new Order<>("AAAA", "aaaa", 100);
        Integer orderType = order.getOrderType();
        System.out.println(orderType);

        SubOrder1 subOrder1 = new SubOrder1();
        subOrder1.setOrderType(120);
        System.out.println(subOrder1.getOrderType());

        SubOrder2<String> subOrder2 = new SubOrder2();
        subOrder2.setOrderType("100");
        System.out.println(subOrder2.getOrderType());
    }
}

/**
 * 自定义泛型类,泛型类型是在实例化对象的时候确定的
 * @param <T>
 */
class Order<T> {
    private String orderId;
    private String orderName;
    private T orderType;

    public Order() {

    }

    public Order(String orderId, String orderName, T orderType) {
        this.orderId = orderId;
        this.orderName = orderName;
        this.orderType = orderType;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getOrderName() {
        return orderName;
    }

    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }

    public T getOrderType() {
        return orderType;
    }

    public void setOrderType(T orderType) {
        this.orderType = orderType;
    }
}

/**
 * 此时SubOrder1并不是一个泛型类
 */
class SubOrder1 extends Order<Integer> {
}

/**
 * 此时SubOrder2继承了父类的泛型类型
 * @param <T>
 */
class SubOrder2<T> extends Order<T> {

}

 

自定义泛型类

原文:https://www.cnblogs.com/liuyang-520/p/15227267.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!