泛型类派生子类

test

public class Test {
    public static void main(String[] args) {
        ChildFirst<String> childFirst = new ChildFirst<>();
        childFirst.setValue("abc");
        String value1 = childFirst.getValue();
        System.out.println(value1);
        System.out.println("============================================");


        Childsecond childsecond = new Childsecond();
        childsecond.setValue(123);
        Integer value2 = childsecond.getValue();
        System.out.println(value2);
}

父类1

package com.fanxing.Demo03;

public class Parent<E> {
    private E value;

    public E getValue() {
        return value;
    }

    public void setValue(E value) {
        this.value = value;
    }
}

子类1

package com.fanxing.Demo03;

/**
 * 泛型类的子类也是泛型类,子类和父类的泛型标记要一只
 *
 * @author liu
 */
public class ChildFirst<T> extends Parent<T> {

    @Override
    public T getValue() {
        return super.getValue();
    }
}

子类二

package com.fanxing.Demo03;

/**
 *
 * 当子类不是泛型类、父类是泛型类,父类必须明确类型,不能形式参数
 *
 * @author liu
 */
public class Childsecond extends Parent<Integer>{
    @Override
    public Integer getValue() {
        return super.getValue();
    }

    @Override
    public void setValue(Integer value) {
        super.setValue(value);
    }
}

posted @ 2022-03-01 16:44  小幼虫虫  阅读(56)  评论(0)    收藏  举报