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
|
class Parent {}
class Subclass extends Parent {}
class Sub extends Parent{}
public class Test {
public static void main(String[] args) {
Collection<Sub> subs = new ArrayList<Sub>();
subs.add(new Sub());
Collection<Subclass> scs = new ArrayList<Subclass>();
scs.add(new Subclass());
//可以直接调用function方法,因为function方法的限定符指定了只要继承Parent类就可以
extFunc(subs);
extFunc(scs);
Collection<Parent> pars = new ArrayList<Parent>();
//必须传递父类
supFunc(pars);
}
//限制父类,必须继承这个父类才能传递
public static void extFunc(Collection<? extends Parent>){
//TODO
}
//限制子类,必须传递它的父类才可以
public static void supFunc(Collection<? super Subclass>){
//TODO
}
}
|