• 自定义泛型类


    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> {
    
    }
    相识是缘
  • 相关阅读:
    css3 preserve-3d 的理解 注意IOS上的兼容
    javascript JSMpeg.js 播放视频解决不用全屏也能播放(也支持自动播放哦)
    linux写系统服务的方法
    mysql connect refuse解决方法
    SQLite-CONSTRAINTS(约束)
    Java集合
    自定义一个简单的SegmentedControl
    symbolicatecrash解析crash文件
    django应用的测试
    WordPress调用page页面内容方法
  • 原文地址:https://www.cnblogs.com/liuyang-520/p/15227267.html
Copyright © 2020-2023  润新知