抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

一、什么是 Java 的泛型?

Java 的泛型是 jdk1.5 引入的一个新特性,其特点就是将类型进行一个参数化,把类型作为参数传递。一些常见的泛型类型有:

E 元素(Element),多用于 Java 集合框架
K 关键字(key)
V 值(value)
T 类型(Type)
N 数字(Number)

语法:

1
<T,...> //T 称为类型占位符,表示一种引用类型 

好处:

  1. 提高代码的重用性

  2. 防止类型转换异常,提高代码的安全性

二、泛型的常见使用形式

1、泛型类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* 泛型类
* 语法:类名 <T>
* T 是类型占位符,表示一种引用类型,如果编写多个使用逗号隔开
* @author: wb
*/
public class MyGeneric<T> {
// 使用泛型 T
//1 创建变量
T t;

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

//3 使用泛型作为方法的返回值
public T getT(){
return t;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* @author: wb
*/
public class TestGeneric {
public static void main(String[] args) {
// 使用泛型类创建对象
// 注意:1 泛型只能使用引用类型 2 不同泛型对象之间不能相互赋值
MyGeneric<String> myGeneric = new MyGeneric<>();
myGeneric.t = "hello world";
myGeneric.show(" 字符串泛型 ");
String string = myGeneric.getT();
System.out.println(string);

MyGeneric<Integer> myGeneric2 = new MyGeneric<>();
myGeneric2.t = 1024;
myGeneric2.show(65536);
Integer integer = myGeneric2.getT();
System.out.println(integer);

}
}

打印结果:

1
2
3
4
 字符串泛型 
hello world
65536
1024

2、泛型接口

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 泛型接口
* 语法:接口名 <T>
* 注意:不能创建泛型静态常量
* @author: wb
*/
public interface MyInterface<T> {
String name = " 张三 ";

T server(T t);
}

泛型接口的实现方式一:

1
2
3
4
5
6
7
8
9
10
11
/**
* @author: wb
*/
public class MyInterfaceImpl implements MyInterface<String> {
@Override
public String server(String s) {
System.out.println(s);
return s;
}
}

泛型接口的实现方式二:

1
2
3
4
5
6
7
8
9
10
11
/**
* @author: wb
*/
public class MyInterfaceImpl2<T> implements MyInterface<T> {
@Override
public T server(T t) {
System.out.println(t);
return t;
}
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @author: wb
*/
public class TestGeneric {
public static void main(String[] args) {
// 泛型接口
MyInterfaceImpl impl = new MyInterfaceImpl();
impl.server("hello world");

MyInterfaceImpl2<Integer> impl2 = new MyInterfaceImpl2();
impl2.server(1024);
}
}

控制台打印结果:

hello world
1024

3、泛型方法

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 泛型方法
* 语法:<T> 返回值类型
* @author: wb
*/
public class MyGenericMethod {
// 泛型方法
public <T> T show(T t){
System.out.println(" 泛型方法 " + t);
return t;
}
}

调用方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class TestGeneric {
public static void main(String[] args) {
// 泛型方法
MyGenericMethod myGenericMethod = new MyGenericMethod();
myGenericMethod.show(" 我的祖国 ");
myGenericMethod.show(123);
myGenericMethod.show(3.14);
}
}
/**
* 控制台打印结果:
* 泛型方法我的祖国
* 泛型方法 123
* 泛型方法 3.14
*/

3、泛型集合

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

  • 编译时检查,而非运行时抛出异常。

  • 访问时,不必类型转换

  • 不同泛型引用之间不能相互赋值,Java 集合中没有

示例:

1
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
/**
* @author: wb
*/
public class Demo3 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("xxx");
arrayList.add("yyy");

for (String s : arrayList) {
System.out.println(s);
}

ArrayList<Student> arrayList2 = new ArrayList<Student>();
Student s1 = new Student(" 张三 ", 21);
Student s2 = new Student(" 里斯 ", 23);
Student s3 = new Student(" 王五 ", 24);
arrayList2.add(s1);
arrayList2.add(s2);
arrayList2.add(s3);
Iterator<Student> it = arrayList2.iterator();
while (it.hasNext()) {
Student next = it.next();
System.out.println(next);
}

}
}
/**
* 学生类
* @author wb
*/
class Student{
private String name;
private String age;
public Student(String name, int age){
this.name = name;
this.age = age;
}
public void setName(String name){
this.name =name;
}
public Stirng getName(String name){
return this.name;
}
}

评论区:

分享你的观点