《如何定义一个Java泛型类》

1、标题

深入理解Java泛型:自定义泛型类的实现方法

2、正文

在Java编程中,泛型是一项非常强大和重要的特性,它可以使我们编写更加通用和灵活的代码。而自定义一个Java泛型类是学习泛型的重要一步,本文将介绍如何自定义一个Java泛型类。

3、代码

①传一个参数的泛型类

定义一个Java输出类,打印构造方法打印的值

public class Print {
    Integer content;
    public Print(Integer content) {
        this.content = content;
    }
    public void print() {
        System.out.println(content);
    }
}

测试类,但是该测试类,只能传入Integer类型,如果想传入字符串类型,引用类型则需要重新定义输出类

public class Test {
    public static void main(String[] args) {
        Print print = new Print(12321);
        print.print();
    }
}

为了使一个输出类可以传入不同的类型进行输出,就要使用到泛型

public class Print<T> {
    T content;
    public Print(T content) {
        this.content = content;
    }
    public void print() {
        System.out.println(content);
    }
}

测试类,通过构造泛型类,则可以传字符串、包装类、引用类型

public class Test {
    public static void main(String[] args) {
        // 传字符串
        Print<String> print1 = new Print<String>("字符串");
        print1.print();
        // 传包装类
        Print<Integer> print2 = new Print<Integer>(12313);
        print2.print();
        // 传引用类型
        Print<User> print3 = new Print<User>(new User("张三"));
        print3.print();
    }
}
②传两个参数的泛型类

有时候需要传两个参数,这两个参数不固定参数类型,则需要定义具备两个参数的泛型类

public class Print<T,K> {
    T content;
    K desc;
    public Print(T content,K desc) {
        this.content = content;
        this.desc=desc;
    }
    public void print() {
        System.out.println("参数值:"+content.toString()+"描述:"+desc.toString());
    }
}

测试,传不同的参数值后输出

public class Test {
    public static void main(String[] args) {
        // 传字符串
        Print<String, String> print1 = new Print<String, String>("字符串", "这是字符串");
        print1.print();
        // 传包装类
        Print<Integer, String> print2 = new Print<Integer, String>(12313, "这是包装类");
        print2.print();
        // 传引用类型,声明User是张三,且年龄是12岁
        Print<User, Integer> print3 = new Print<User, Integer>(new User("张三"), 12);
        print3.print();
    }
}
③有界限的泛型

定义了一个有界限的泛型类,传入的参数必须是Vehicle的子类

public class Print<T extends Vehicle> {
    T content;
    public Print(T content) {
        this.content = content;
    }
    public void print() {
        content.run();
    }
}

测试类,传入Vehicle的子类

public class Test {
    public static void main(String[] args) {
        // User不是Vehicle的子类
//        Print<User> print1 = new Print<User>(new User("张三"));
//        print1.print();
        // 传入Vehicle的子类
        Print<Car> print2 = new Print<Car>(new Car());
        print2.print();
        // 传入Vehicle的子类
        Print<Bus> print3 = new Print<Bus>(new Bus());
        print3.print();
    }
}
④ 定义一个泛型方法

没有对泛型方法进行约束

public class Test {
    public static void main(String[] args) {
        print("字符串");
        print(12312);
        print(new User("张三"));
    }
    public static <T> void print(T content) {
        System.out.println(content.toString());
    }
}

对泛型方法进行约束,传入的不是Vehicle的子类则会报错

public class Test {
    public static void main(String[] args) {
        print("字符串");
        print(12312);
        print(new User("张三"));
        print(new Car());
    }
    public static <T extends Vehicle> void print(T content) {
        System.out.println(content.toString());
    }
}

在这里插入图片描述

⑤ 泛型界定
/**
 * 交通工具的父类
 *
 * @date 2023/11/17
 */
public class Desc {
    void desc() {
        System.out.println("都在路上跑");
    }
}

/**
 * 交通工具的接口类
 *
 * @date 2023/11/17
 */
public interface Vehicle {

    void run();
}

/**
 * 小汽车类
 * @date 2023/11/17
 */
public class Car extends Desc implements Vehicle {

    @Override
    public void run() {
        System.out.println("小汽车");
    }
}

/**
 * 交通工具类
 *
 * @date 2023/11/17
 */
public class Bus extends Desc implements Vehicle {

    @Override
    public void run() {
        System.out.println("公交车");
    }
}

/**
 * 自行车
 *
 * @date 2023/11/17
 */
public class Bike implements Vehicle {
    @Override
    public void run() {
        System.out.println("自行车");
    }
}
public class Test {
    public static void main(String[] args) {
        print(new Car());
        print(new Car());
        print(new Bike());
    }

    /**
     * 限定 T 必须实现 Desc 接口并继承 Vehicle 类
     *
     * @param content
     */
    public static <T extends Desc & Vehicle> void print(T content) {
        System.out.println(content.toString());
    }
}

在这里插入图片描述

⑥ 使用通配符定义一个泛型方法

使用通配符?表示未知类型

public class Test {
    public static void main(String[] args) {
        List<String> list1 = new ArrayList<>();
        list1.add("字符串");
        print(list1);

        List<Integer> list2 = new ArrayList<>();
        list2.add(123);
        print(list2);

        List<User> list3 = new ArrayList<>();
        list3.add(new User("张三"));
        print(list3);

    }

    /**
     * 使用通配符?表示位置类型
     *
     * @param content
     */
    public static void print(List<?> content) {
        System.out.println(content.toString());
    }
    
    /**
     * 使用通配符?,限定参数只能是Vehicle的子类
     *
     * @param content
     */
    public static void print(List<? extends Vehicle> content) {
        System.out.println(content.toString());
    }
    
    /**
     * 使用通配符?,限定参数必须是Car本身或者是Car的父类
     *
     * @param content
     */
    public static void print(List<? super Car> content) {
        System.out.println(content.toString());
    }
}

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术路上的探险家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值