一、什么是 Java 的泛型?
Java 的泛型是 jdk1.5 引入的一个新特性,其特点就是将类型进行一个参数化,把类型作为参数传递。一些常见的泛型类型有:
E 元素(Element),多用于 Java 集合框架
K 关键字(key)
V 值(value)
T 类型(Type)
N 数字(Number)
语法:
好处:
-
提高代码的重用性
-
防止类型转换异常,提高代码的安全性
二、泛型的常见使用形式
1、泛型类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
public class MyGeneric<T> { T t;
public void show(T t){ System.out.println(t); }
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
|
public class TestGeneric { public static void main(String[] args) { 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
|
public interface MyInterface<T> { String name = " 张三 ";
T server(T t); }
|
泛型接口的实现方式一:
1 2 3 4 5 6 7 8 9 10 11
|
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
|
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
|
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
|
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); } }
|
3、泛型集合
概念:参数化类型的集合、类型安全的集合,强制集合元素的类型必须一致。优点:
示例:
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
|
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); }
} }
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; } }
|