集合框架 第2天(泛型概述,泛型类,泛型接口,泛型方法,泛型集合)

1. 泛型概述

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

2. 泛型类

  • 注意:
    • 泛型只能是引用类型
    • 不同的泛型对象不能相互赋值
public class Test {
    public static void main(String[] args) {
        // 使用泛型类创建对象
        TestGenericClass<String> testGeneric1 = new TestGenericClass<String>();
        testGeneric1.t = "ABC";
        testGeneric1.show("abc");
        System.out.println(testGeneric1.getT());

        TestGenericClass<Integer> testGeneric2 = new TestGenericClass<>();
        testGeneric2.t = 10;
        testGeneric2.show(100);
        System.out.println(testGeneric2.getT());
    }
}
public class TestGenericClass<T> {
    // 使用泛型
    // 创建变量
    T t;

    // 作为方法参数
    public void show(T t) {
        System.out.println(t);
    }

    // 作为方法返回值
    public T getT() {
        return t;
    }
}

3. 泛型接口

  • 注意:
    • 不能使用泛型创建静态常量
public class Test {
    public static void main(String[] args) {
        TestInterface1 testInterface1 = new TestInterface1();
        System.out.println(testInterface1.server("abc"));

        TestGenericInterface<String> testGeneric = new TestInterface1();
        System.out.println(testGeneric.server("qwe"));

        TestInterface2<Integer> testInterface2 = new TestInterface2<>();
        System.out.println(testInterface2.server(999));
    }
}
public class TestInterface1 implements TestGenericInterface<String> {
    @Override
    public String server(String s) {
        System.out.println(s);
        return s + "123";
    }
}
public class TestInterface2<T> implements TestGenericInterface<T> {
    @Override
    public T server(T t) {
        System.out.println(t);
        return t;
    }
}
public interface TestGenericInterface<T> {
    String name = "ABC";

    T server(T t);
}

4. 泛型方法

public class Test {
    public static void main(String[] args) {
        TestGenericMethod testGenericMethod = new TestGenericMethod();
        testGenericMethod.show("ABC");
        testGenericMethod.show(123);
    }
}
public class TestGenericMethod {
    // 泛型方法
    public <T> T show(T t) {
        System.out.println("ABC" + t);
        return t;
    }
}

5. 泛型集合

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

public class Test {
    public static void main(String[] args) {
        ArrayList<String> arrayList1 = new ArrayList<>();
        arrayList1.add("ABC");
        arrayList1.add("abc");
        for (String str : arrayList1) {
            System.out.println(str);
        }

        ArrayList<Student> arrayList2 = new ArrayList<>();
        Student student1 = new Student("A", 10);
        Student student2 = new Student("B", 20);
        Student student3 = new Student("C", 30);
        arrayList2.add(student1);
        arrayList2.add(student2);
        arrayList2.add(student3);

        Iterator<Student> iterator = arrayList2.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

    }
}

class Student {
    String name;
    int age;

    Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值