1. 程式人生 > 其它 >自定義泛型類

自定義泛型類

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> { }
相識是緣