-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClassTypeCapture.java
More file actions
42 lines (31 loc) · 940 Bytes
/
ClassTypeCapture.java
File metadata and controls
42 lines (31 loc) · 940 Bytes
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
package generics;
import java.util.*;
/**
* RUN:
* javac generics/ClassTypeCapture.java && java generics.ClassTypeCapture
* OUTPUT:
* true
* true
* false
* true
*/
// COMPILETIME ERROR !!!
public class ClassTypeCapture<T> {
Class<T> kind;
public ClassTypeCapture(Class<T> kind) {
this.kind = kind;
}
public boolean f(Object arg) {
return kind.isInstance(arg);
}
public static void main(String[] args) {
ClassTypeCapture<Building> ctt2 = new ClassTypeCapture<Building>(Building.class);
System.out.println(ctt2.f(new Building()));
System.out.println(ctt2.f(new House()));
ClassTypeCapture<House> ctt3 = new ClassTypeCapture<House>(House.class);
System.out.println(ctt3.f(new Building()));
System.out.println(ctt3.f(new House()));
}
}
class Building {}
class House extends Building {}