内容来自张孝祥老师的java基础加强
加上自己的理解做笔记整理:
如果我们不能确定集合的类型,可以定义为?,它可以代表任意类型
public void printCollection(Collection<?> collection)
当然也可以:
public <T> void printCollection(Collection<T> collection)
需求:想表示多个类型,但是又不是所以类型都可以,要表示某个区间的类型
解决方案:
l限定通配符的上边界:
Ø正确:Vector<? extends Number>x = new Vector<Integer>(); // ?必须继承Number,也就是Number的子类
Ø错误:Vector<? extends Number>x = new Vector<String>();
l限定通配符的下边界:
Ø正确:Vector<? super Integer>x = new Vector<Number>(); // ?必须是Integer的超类,super
Ø错误:Vector<? super Integer>x = new Vector<Byte>();
继承还可以是多个接口,要用&号连接