- 使用场景示例
public class Test01 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList();
arrayList.add("str1");
arrayList.add(1);
}
}
上述的arrayList是使用泛型的过程中非常常见的例子,arrayList指定了接收的元素类型为String,如果往arrayList添加其他类型元素,则会报编译异常。
如果在new ArrayList的时候,没有指定元素类型
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add("str1");
arrayList.add(10);
System.out.println(arrayList.get(0).getClass());
System.out.println(arrayList.get(1).getClass());
}
那么ArrayList则默认接收Object类型元素,这也是jdk1.5之前,java还没有泛型机制时,集合元素的存储类型。
如果对不加泛型的arrayList做强制类型转换
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add("str1");
arrayList.add("str2");
arrayList.add(10);
Iterator iterator = arrayList.iterator();
while(iterator.hasNext()){
String next = (String) iterator.next();
System.out.println(next);
}
}
会出现类型转换错误
2. 泛型类定义
泛型定义格式:修饰符 class 类名<类型>{}
例子: public class MyClass{}
T可以写为任意标识,常用T、E、K、V等型式的参数来标识泛型
public class Student<T> {
/**
* 成员变量t的类型和泛型T的类型相同
*/
private T t;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public T show(T t){
return t;
}
}
public static void main(String[] args) {
Student<String> stringStudent = new Student<>();
stringStudent.setT("str1");
stringStudent.show("str1");
Student<Integer> integerStudent = new Student<>();
integerStudent.setT(10);
integerStudent.show(100);
}
Student类可以指定不同类型的成员变量t,不用针对String,Integer等不同类型的参数实现多个get,set,show方法。
- 泛型方法
格式:修饰符 <类型> 返回值类型 方法名(类型 变量名){…}
例子:public void show (T t) {…}
Student student = new Student();
System.out.println(student.show("str"));
System.out.println(student.show(10));
System.out.println(student.show(3.1415926));
4.泛型接口
格式: 修饰符 interface 接口名 <类型> {…}
范例: public interface MyInterface {…}
public interface MyInterface<T> {
public T show(T t);
}
public class MyInterfaceImpl<T> implements MyInterface<T> {
@Override
public T show(T t) {
return t;
}
}
- 泛型接口和和泛型方法
同时使用泛型方法和泛型接口时,方法的类型标识值不能和接口的类型标识值相同,所以例子中,接口的类型标识为T,方法的类型标识为M。
public interface MyInterface<T> {
public<M> T show(T t,M m);
}
public class MyInterfaceImpl<T> implements MyInterface<T> {
@Override
public<M> T show(T t,M m) {
return t;
}
}
- 泛型<?>
public class Test01 {
public static void main(String[] args) {
List<String> stringList = new ArrayList<>();
stringList.add("str1");
printList(stringList);
List<Integer> integerList = new ArrayList<>();
integerList.add(10);
printList(integerList);
}
/**
* List<?>只能用于接收和返回,不能用于添加
* @param list
*/
public static void printList(List<?> list){
Iterator iterator = list.listIterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
- 类型通配符上下限
上限:< ? extends Parent> 可以接收Parent和Parent子类
public static void printList(List<? extends Parent> list){
Iterator iterator = list.listIterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
public class Parent {
}
public class Children extends Parent{
}
下限:<? super Children> 可以用Children或者它的父类型
/**
* 运行Parent及其父类作为参数
* @param list
*/
public static void printList2(List<? super Parent> list){
Iterator<? super Parent> iterator = list.listIterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
/**
* 运行Parent及其父类作为参数
* @param list
*/
public static void printList2(List<? super Parent> list){
Iterator<? super Parent> iterator = list.listIterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
public class Parent extends GrandParent{
}
- 泛型擦除
public class Student<T> {
/**
* 成员变量t的类型和泛型T的类型相同
*/
private T t;
public T getT() {
return t;
}
}
Student.java编译得到的Class文件为
public class Student {
private Object t;
public Object getT() {
return t;
}
}
再将.class文件反编译为.java文件后,泛型丢失。
- 方法返回值加和不加T
方法返回值前的的作用是告诉编译器,当前的方法的值传入类型可以和类初始化的泛型类不同,也就是该方法的泛型类可以自定义,不需要跟类初始化的泛型类相同。
class Show<T> {
public void print1(T t) {
System.out.println(t);
}
public <T> void print2(T t) {
System.out.println(t);
}
}
public class Demo {
public static void main(String[] args) {
Show<String> show = new Show<String>();
show.print1(new Integer(1));// 不能编译
show.print2(new Integer(1));// 可以编译
}
}
Show类定义了泛型T,它有两个print方法,两个方法的差别就是print1没有,而print2有。
Demo类实例化Show类,并将泛型类型定义为String类型,却为两个print方法传入Integer类型的对象,print2可以编译,而print1不可以编译。
print1中的泛型与show对象的泛型相同,都是String,因此不能传入Integer类型的参数。而print2方法在返回值前定义了一个泛型T,因此方法参数类型不受对象泛型类型限制,这样定义的话这个方法是可以传入任意类型参数的。