package com.bjpowernode.t02generic;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/*
* 泛型的通配符
*/
public class TestGeneric03 {
public static void main(String[] args) {
ArrayList<String> l1 = new ArrayList<>();
LinkedList<Integer> l2 = new LinkedList<>();
List<Double> l3 = new LinkedList<>();
List<Object> l4 = new ArrayList<>();
List<Number> l5 = new ArrayList<>();
//可以像集合中添加泛型类型的子类
l4.add("String");
print4(l5);
}
//带有下边界的通配符,只能传入泛型是Integer或其父类型
public static void print4(List<? super Integer> list) {
}
//带有上边界的通配符,只能传入泛型是Number或其子类型
public static void print3(List<? extends Number> list) {
}
//通用的遍历List的方法
//使用?表示泛型的通配符
public static void print2(List<?> list) {
for(Object obj:list) {
System.out.println(obj);
}
}
public static void print1(List list) {
for(Object obj:list) {
System.out.println(obj);
}
}
}