自定义泛型类

自定义泛型类

直接上实践吧

实践

package com.atguigu.generic;

/**
 * 功能描述
 *
 * @since 2022-05-21
 */
public class CustomizeGenericClassDemo<T> {
    private String name;

    private int num;

    private T t;

    public CustomizeGenericClassDemo() {
    }

    public CustomizeGenericClassDemo(String name, int num, T t) {
        this.name = name;
        this.num = num;
        this.t = t;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }

    @Override
    public String toString() {
        return "CustomizeGenericClassDemo{" +
                "name='" + name + '\'' +
                ", num=" + num +
                ", t=" + t +
                '}';
    }

    public static void main(String[] args) {
        CustomizeGenericClassDemo<String> classDemo = new CustomizeGenericClassDemo<String>();
        classDemo.setT("wahaha");
        classDemo.setNum(12);
        classDemo.setName("drink");
        System.out.println(classDemo);

        CustomizeGenericClassDemo<Integer> classDemo1 = new CustomizeGenericClassDemo<>();
        classDemo1.setT(500);
        classDemo1.setNum(12);
        classDemo1.setName("drink");
        System.out.println(classDemo1);

    }
}

package com.atguigu.generic;

/**
 * 功能描述
 *
 * @since 2022-05-21
 */
public class SubCustomizeGenenricClassDemo extends CustomizeGenericClassDemo<String> {
    public static void main(String[] args) {
        SubCustomizeGenenricClassDemo subCustomizeGenenricClassDemo = new SubCustomizeGenenricClassDemo();
        subCustomizeGenenricClassDemo.setT("ahaha");
        subCustomizeGenenricClassDemo.setNum(56);
        subCustomizeGenenricClassDemo.setName("no");
        System.out.println(subCustomizeGenenricClassDemo);

    }
}

package com.atguigu.generic;

/**
 * 功能描述
 *
 * @since 2022-05-21
 */
public class SubCustomizeGenenricClassDemo1<T> extends CustomizeGenericClassDemo<T> {
    public static void main(String[] args) {
        SubCustomizeGenenricClassDemo1<String> subCustomizeGenenricClassDemo = new SubCustomizeGenenricClassDemo1();
        subCustomizeGenenricClassDemo.setT("ahaha");
        subCustomizeGenenricClassDemo.setNum(56);
        subCustomizeGenenricClassDemo.setName("no");
        System.out.println(subCustomizeGenenricClassDemo);

        SubCustomizeGenenricClassDemo1<Integer> subCustomizeGenenricClassDemo1 = new SubCustomizeGenenricClassDemo1();
        subCustomizeGenenricClassDemo1.setT(70);
        subCustomizeGenenricClassDemo1.setNum(56);
        subCustomizeGenenricClassDemo1.setName("no");
        System.out.println(subCustomizeGenenricClassDemo1);

    }
}
posted @ 2022-05-21 11:25  Oh,mydream!  阅读(18)  评论(0编辑  收藏  举报