# 第七章 泛型
# 7.1 泛型引入
编写程序在 ArrayList 中添加 3 个 Dog 对象
Dog 对象含有 name 和 age,并输出 name 和 age(要求使用 getXxx())
先使用传统的方法来解决
public class Generic01 {
public static void main(String[] args) {
//使用传统方法解决
ArrayList arrayList = new ArrayList();
arrayList.add(new Dog("旺财", 10));
arrayList.add(new Dog("发财", 1));
arrayList.add(new Dog("大黄", 5));
//假如程序员不小心添加了一只猫,会导致遍历时的向下转型异常
arrayList.add(new Cat("招财", 8));
//遍历
for (Object o : arrayList) {
//向下转型 Object 转为 Dog
Dog dog = (Dog) o;
System.out.println(dog.getName() + "-" + dog.getAge());
}
}
}
class Dog {
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Cat {
private String name;
private int age;
public Cat(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 7.2 泛型入门
# 7.2.1 使用传统方法的问题分析
不能对加入到集合 ArrayList 中的数据类型进行约束(不安全)
遍历的时候需要进行类型转换,如果集合中的数据量较大会对效率有影响
可以使用泛型解决这些问题
# 7.2.2 泛型的好处
编译时,检查添加元素的类型,提高了安全性
减少了类型转换的次数,提高效率
当不使用泛型时:Dog 类型加入集合时会被转成 Object 类型,取出时又要转成 Dog 类型
使用泛型时:Dog 类型加入集合时转成泛型指定的类型即 Dog,取出时还是 Dog 类型
不再提示编译警告
//使用泛型
public class Generic02 {
public static void main(String[] args) {
//使用泛型解决
//ArrayList<Dog> 表示存放到 ArrayList 集合中的元素是 Dog 类型
//如果编译器发现添加的类型不满足需求,就会报错
//在遍历时,可以直接取出 Dog 类型而不是 Object
ArrayList<Dog2> arrayList = new ArrayList<Dog2>();
arrayList.add(new Dog2("旺财", 10));
arrayList.add(new Dog2("发财", 1));
arrayList.add(new Dog2("大黄", 5));
//使用泛型后,就不会出现一不小心添加了一个 Cat2 对象的情况
//因为一旦想要添加除了 Dog2 以外的对象 idea 就会报错
//arrayList.add(new Cat2("喵喵", 8));
System.out.println("===使用泛型===");
for (Dog2 dog2 : arrayList) {
System.out.println(dog2.getName() + " - " + dog2.getAge());
}
}
}
class Dog2 {
private String name;
private int age;
public Dog2(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Cat2 {
private String name;
private int age;
public Cat2(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 7.3 泛型基本说明
/**
* 泛型的作用是:可以在类声明时通过一个标识表示类中某个属性的类型,或者是某个方法的返回值的类型,或者是参数类型
* */
public class Person<E> {
E s; //E 表示 s 的数据类型,该数据类型在定义 Person 对象的时候指定,即在编译期间就确定 E 是什么类型
public Person(E s) { //E 也可以是参数类型
this.s = s;
}
public E f() { //返回类型使用 E
return s;
}
public static void main(String[] args) {
Person<String> person = new Person<>("止束");
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
泛型,即广泛的类型
泛型又称参数化类型,是 JDK5.0 出现的新特性,解决数据类型的安全性问题
在类声明或实例化时只要指定好需要的具体的类型即可
Java 泛型可以保证如果程序在编译时没有发出警告,运行时就不会产生 ClassCastException 异常,同时,代码更加简洁、健壮
泛型的作用是:可以在类声明时通过一个标识表示类中某个属性的类型,或者是某个方法的返回值的类型,或者是参数类型。
# 7.4 泛型使用细节
interface List<T>{}
public class HashSet<E>{}
等,其中 T,E 只能是引用类型,例如:
List<Integer> list = new ArrayList<Integer>(); //正确,因为 Integer 是引用类型
List<int> list2 = new ArrayList<int>(); //错误,因为 int 不是引用类型
2
在指定泛型具体类型后,可以传入该类型或者其子类类型
泛型使用形式
List<Integer> list1 = new ArrayList<Integer>();
List<Integer> list2 = new ArrayList<>();
//如果啥也不写默认是 Object
List list3 = new ArrayList();
//相当于
List<Object> list3 = new ArrayList<Object>();
2
3
4
5
6
# 7.5 自定义泛型
# 7.5.1 自定义泛型类
- 基本语法
class 类名<T, R, ...>{ //...表示可以有多个泛型
成员
}
2
3
普通成员可以使用泛型(属性、方法)
使用泛型的数组,不能初始化
静态方法中不能使用类的泛型
泛型类的类型,是在创建对象时确定的(因为创建对象时,需要指定确定的类型)
如果在创建对象时,没有指定类型,默认为 Object
class Tiger<T, R, M> {
String name;
R r;
M m;
T t;
public Tiger(String name) {
this.name = name;
}
public void setT(T t) {
this.t = t;
}
public T getT() {
return t;
}
public void setM(M m) {
this.m = m;
}
public M getM() {
return m;
}
public void setR(R r) {
this.r = r;
}
public R getR() {
return r;
}
}
public class Test {
public static void main(String[] args) {
//T=Double, R=String, M=Integer
Tiger<Double, String, Integer> g = new Tiger<>("john");
g.setT(10.9); //对的
g.setT("yy"); //错误,类型不对
//T=Object, R=Object, M=Object
Tiger g2 = new Tiger("john..");
g2.setT("yy"); //对的,因为 T=Object
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 7.5.2 自定义泛型接口
- 基本语法
interface 接口名<T, R, ...> {
}
2
3
接口中,静态成员也不能使用泛型(这个和泛型类的规定一样)
泛型接口的类型,在继承接口或者实现接口是确定
没有指定类型,默认为 Object
# 7.5.3 自定义泛型方法
- 基本语法
修饰符 <T, R, ...> 返回类型 方法名(参数列表) {
}
2
泛型方法,可以定义在普通类中,也可以定义在泛型类中
当泛型方法被调用时,类型会确定
public void eat(E e) {}
,修饰符后没有 <T, R, ...>,说明 eat 方法不是泛型方法,而是使用了泛型
# 7.6 泛型继承和通配
- 泛型不具备继承性
List<Object> list = new ArrayList<String>(); //错误
当这样写时:
<?>
:支持任意泛型类型当这样写时:
<? extends A>
:支持 A 类以及 A 类的子类,规定了泛型的上限当这样写时:
<? super A>
:支持 A 类以及 A 类的父类,不限于直接父类,规定了泛型的下限
public static void printCollection1(List<?> c) {} //通配符,取出时就是 Object
public static void printCollection2(List<? extends AA> c) {} //表示可以接受 AA 或者 AA 子类
public static void printCollection3(List<? super AA> c) {} //表示可以接受 AA 或者 AA 的父类
2
3