当前位置:网站首页>4.泛型和工具类

4.泛型和工具类

2022-08-09 09:23:00 过来我的小熊

Java泛型和工具类

泛型

  • 概述:Java泛型是JDK1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递
  • 常见的形式有泛型类,泛型接口,泛型方法
  • 语法:<T,…> T称为类型占位符,表示一种引用类型
  • 好处:可以提高代码的复用性,防止类型转换异常,提高代码的安全性

泛型类

语法:类名 T是类型占位符,表示一种引用类型,如果写多个使用逗号隔开

注意:

  • 泛型只能是引用类型
  • 不同泛型对象之间不能相互赋值
package com.jhkj.fx;

/**
 * Generic 一般的,通用的
 * 泛型类
 * 语法:类名<T(类型)>  T是类型占位符,表示一种引用类型,可以编写多个,如果写多个使用逗号隔开
 */
public class MyGeneric<T> {
    // 使用泛型T
    // 1.创建变量
    T t;
    // 2.作为方法参数
    public void show(T t){
        System.out.println(t);
    }
    // 3.作为返回值
    public T getT(){
        return t;
    }
}

package com.jhkj.fx;

/**
 * 使用泛型类
 * 注意:
 *      1.泛型只能使用引用类型
 *      2.不同泛型对象之间不能相互赋值
 */
public class TestMyGeneric {
    public static void main(String[] args) {
        // 创建泛型类对象
        MyGeneric<String> stringMyGeneric = new MyGeneric<String>();
        stringMyGeneric.t = "白书";
        stringMyGeneric.show("加油");
        String t = stringMyGeneric.getT();
        System.out.println(t);

        MyGeneric<Integer> integerMyGeneric = new MyGeneric<>();
        integerMyGeneric.t = 17;
        integerMyGeneric.show(1);
        Integer t1 = integerMyGeneric.getT();
        System.out.println(t1);
    }
}

泛型接口

语法:接口名 T为类型占位符,表示一种引用类型

注意:不能创建泛型静态常量

泛型接口的实现类可以为普通类,也可以为泛型类

泛型接口

package com.jhkj.fx;

public class MyInterfaceImpl implements MyInterface<String>{
    @Override
    public String service(String s) {
        System.out.println(s);
        return s;
    }
}

实现类 (普通类)

package com.jhkj.fx;

public class MyInterfaceImpl implements MyInterface<String>{ // 直接确定类型
    @Override
    public String service(String s) {
        System.out.println(s);
        return s;
    }
}

实现类(泛型类)

package com.jhkj.fx;

public class MyInterfaceImpl2<T> implements MyInterface<T> { // 在给泛型类传递泛型参数的同时实现接口
    @Override
    public T service(T t) {
        System.out.println(t);
        return t;
    }
}

Test

package com.jhkj.fx;

public class Test {
    public static void main(String[] args) {
        MyInterfaceImpl myInterface = new MyInterfaceImpl();
        myInterface.service("白书");

        MyInterfaceImpl2<Integer> impl2 = new MyInterfaceImpl2<>();
        impl2.service(17);
    }
}

泛型方法

语法:修饰符 返回值类型 方法名(T t)

package com.jhkj.fx;

/**
 * 泛型方法
 * 语法: 修饰符 <T> 返回值类型 方法名(T t)
 */
public class MyGenericMethod {

    // 泛型方法
    public <T> void show(T t){
        System.out.println("泛型方法" + t);
    }
}

package com.jhkj.fx;

public class Test {
    public static void main(String[] args) {
        // 泛型方法
        MyGenericMethod genericMethod = new MyGenericMethod();
        genericMethod.show("白书"); // 可以为任意类型

    }
}

泛型集合

  • 概念:参数化类型,类型安全的集合,强制集合元素的类型必须一致
  • 特点
    • 编译时即可检查,而非运行时抛出异常
    • 访问时,不必类型转换(拆箱)
    • 不同泛型之间引用不能相互赋值,泛型不存在多态
package com.jhkj.fx;

import com.jhkj.collection.Student;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * 泛型集合
 */
public class Demo {
    public static void main(String[] args) {
        ArrayList ts = new ArrayList();

        // 当不写泛型时,是可以添加任何类型元素,但是在转换类型时,还得必须确认之前类型
        ts.add("你");
        ts.add("我");
        ts.add("他");
        ts.add(10);
        ts.add(20);
        for (Object t : ts) {
            // String s = (String) t;
            System.out.println(t);
        }

        ArrayList<Student> students = new ArrayList<>(); // 当泛型指定之后,那输入的数据就必须是泛型类型的数据
        Student s1 = new Student("张三",14);
        Student s2 = new Student("李四",13);
        Student s3 = new Student("王五",12);
        students.add(s1);
        students.add(s2);
        students.add(s3);
        Iterator<Student> iterator = students.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

原网站

版权声明
本文为[过来我的小熊]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_56121715/article/details/123747520