-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWildcards.java
More file actions
109 lines (67 loc) · 2.17 KB
/
Wildcards.java
File metadata and controls
109 lines (67 loc) · 2.17 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package Test_15;
public class Wildcards {
static void rawArgs(Holder holder,Object obj){
holder.set(obj);//Warning
holder.set(new Wildcards());//Warning
}
static void unboundedArg(Holder<?> holder,Object arg){
//holder.set(val);//Error
//holder.set(new Wildcards());//Error
Object obj=holder.get();
}
static <T> T exact1(Holder<T> holder){
T t=holder.get();
return t;
}
static <T> T exact2(Holder<T> holder,T arg){
holder.set(arg);
T t=holder.get();
return t;
}
static <T> T wildSubtype(Holder<? extends T> holder,T arg){
// holder.set(val);//Error
T t=holder.get();
return t;
}
static <T> void wildSupertype(Holder<? super T> holder,T arg){
holder.set(arg);
//T t=holder.get();//Error
//OK ,but type information has been lost;
Object obj=holder.get();
}
public static void main(String[] args){
Holder raw=new Holder<Long>();
//Or:
raw=new Holder();
Holder<Long> qualified=new Holder<Long>();
Holder<?> unbounded=new Holder<Long>();
Holder<? extends Long> bounded=new Holder<Long>();
Long lng=1L;
rawArgs(raw,lng);
rawArgs(qualified,lng);
rawArgs(unbounded,lng);
rawArgs(bounded,lng);
unboundedArg(raw,lng);
unboundedArg(qualified,lng);
unboundedArg(unbounded,lng);
unboundedArg(bounded,lng);
//Object r1=exact1(raw);
//Unchecked conversion from Holder to Holder<T>
// Unchecked method invocation:exact1(Holder<T>) id applied to (Holder)
Long r2=exact1(qualified);
Object r3=exact1(unbounded);//must return Object
Long r4=exact1(bounded);
//Long r5=exact2(raw,lng)//Warning:
//Unchecked conversion from Holder to Holder<Long>
//Unchecked method invocation: exact2(Holder<T>,T)
// is applied to (HOlder,Long)
Long r6=exact2(qualified,lng);
//ÀàÐͲ»Åä
//Long r7=exact2(unbounded,lng);//Error
//Long r8=exact2(bounded,lng);//Error
//Long r9=wildSubtype(raw,lng);//Warning
Long r10=wildSubtype(raw,lng);
Object r11=wildSubtype(unbounded,lng);
Long r12=wildSubtype(bounded,lng);
}
}