Java-----泛型和工具类

本文介绍了Java泛型的概念、语法及其在类、接口和方法中的应用,展示了如何通过泛型实现类型安全的集合,并通过实例演示了泛型在实际开发中的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

泛型和工具类

泛型

  • Java泛型是JDK1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递。

  • 常见形式有泛型类、泛型接口、泛型方法。

  • 语法:

    • <T,…> T 称为类型占位符,表示一种引用类型。
  • 好处:

    • (1)提高代码的重用性
    • (2)放置类型转换异常,提高代码的安全性。
  • 实例一(使用泛型类):

    // 泛型类
    // 语法:类名<T>
    // T是类型占位符,表示一种引用类型,如果编写多个使用逗号隔开
    public class MyGeneric<T>{
        // 使用泛型
        // 创建遍历
        T t;
        
        // 泛型作为方法的参数
        public void show(T t){
            System.out.println(t);
        }
        // 泛型作为方法的返回值
        public T getT(){
            return t;
        }
    }
    
    public class GenericityTest {
        public static void main(String[] args) {
            // 使用泛型类型创建对象
            // 注意:1 泛型只能使用引用类型 2 不同泛型类型对象之间不能相互赋值
            System.out.println("-----------string操作");
            MyGeneric<String> myGeneric = new MyGeneric<String>();
            myGeneric.t = "Hello";
            myGeneric.show("大家好,加油!!!");
            String string = myGeneric.getT();
            System.out.println(string);
    
            System.out.println("-----------Integer操作");
            MyGeneric<Integer> myGeneric1 = new MyGeneric<Integer>();
            myGeneric1.t = 100;
            myGeneric1.show(200);
            Integer integer = myGeneric1.getT();
        }
    }
    
    

  • 实例2(泛型接口):

    // 语法:借口名<T>
    // 不能泛型静态常量
    public interface Myinterface<T>{
        String name = "张三";
        
        T server(T t);
        
    }
    
    public class MyInterfaceImpl implements MyInterface<String>{
        @Override
        public String server (String t){
            System.out.println(t);
            return t;
        }
    }
    
    public class MyInterfaceImpl2<T> implements MyInterface<T>{
        @Override
        public T server(T t){
            System.out.println(t);
            return t;
        }
    }
    
    public class TestGeneric{
        public static void main(String[] arys){
            MyInterfaceImpl impl = new MyInterfaceImpl();
            impl.server("大家好,加油!!!");
            
            MyInterfaceImpl2<Integer> impl2 = new MyInterfaceImpl2<>();
            impl2.server(1000);
        }
    }
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xLbMefBk-1617107900048)(D:\学习\tupian\泛型\fxjk.png)]

  • 实例3(泛型方法):

    // 语法:<T> 返回值类型
    public class MyGenericMethod{
        // 泛型方法
        public <T> T show(T t){
            System.out.println("泛型方法"+t);
            return t;
        }
    }
    
    public class TestGeneric{
        public static boid main(String[] arys){
            // 泛型方法
            // 类型不需要传递,由我们传递的数据来决定。
            MyGenericMethod myGenericMethod = new MyGenericMethod();
            myGenericMethod.show("中国");// String
            myGenericMethod.show(200);// Integer
            myGenericMethod.show(3.14);// Double
        }
    }
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-neCR7WmA-1617107900053)(D:\学习\tupian\泛型\fxff.png)]

泛型集合

  • 概念:参数化类型、类型安全的集合,强制集合元素的类型必须一致。

  • 特点:

    • 编译时即可检查,而非运行时抛出异常。
    • 访问时,不必类型转换(拆箱)。
    • 不同泛型之间引用不能相互赋值,泛型不存在多态。
    public class GenericArrayList {
        public static void main(String[] args) {
            ArrayList<String> arraylIst = new ArrayList<String>();
            arraylIst.add("千里之行");
            arraylIst.add("始于足下");
    
            for (String string:
                 arraylIst) {
                System.out.println(string);
            }
    
            ArrayList<Student> arrayList2 = new ArrayList<Student>();
            Student s1 = new Student("刘德华",20);
            Student s2 = new Student("郭富城",22);
            Student s3 = new Student("梁朝伟",18);
            arrayList2.add(s1);
            arrayList2.add(s2);
            arrayList2.add(s3);
    
            Iterator<Student> it = arrayList2.iterator();
            while(it.hasNext()){
                Student s = it.next();
                System.out.println(s.toString());
            }
    
        }
    }
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rdHrDwM8-1617107900058)(D:\学习\tupian\泛型\s.png)]

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值