2015-04-20 15:36:01 -07:00
|
|
|
|
//: generics/ClassTypeCapture.java
|
2015-05-29 14:18:51 -07:00
|
|
|
|
// <20>2015 MindView LLC: see Copyright.txt
|
2015-04-20 15:36:01 -07:00
|
|
|
|
|
|
|
|
|
class Building {}
|
|
|
|
|
class House extends Building {}
|
|
|
|
|
|
|
|
|
|
public class ClassTypeCapture<T> {
|
|
|
|
|
Class<T> kind;
|
|
|
|
|
public ClassTypeCapture(Class<T> kind) {
|
|
|
|
|
this.kind = kind;
|
|
|
|
|
}
|
|
|
|
|
public boolean f(Object arg) {
|
|
|
|
|
return kind.isInstance(arg);
|
2015-05-18 23:05:20 -07:00
|
|
|
|
}
|
2015-04-20 15:36:01 -07:00
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
ClassTypeCapture<Building> ctt1 =
|
2015-05-05 11:20:13 -07:00
|
|
|
|
new ClassTypeCapture<>(Building.class);
|
2015-04-20 15:36:01 -07:00
|
|
|
|
System.out.println(ctt1.f(new Building()));
|
|
|
|
|
System.out.println(ctt1.f(new House()));
|
|
|
|
|
ClassTypeCapture<House> ctt2 =
|
2015-05-05 11:20:13 -07:00
|
|
|
|
new ClassTypeCapture<>(House.class);
|
2015-04-20 15:36:01 -07:00
|
|
|
|
System.out.println(ctt2.f(new Building()));
|
|
|
|
|
System.out.println(ctt2.f(new House()));
|
|
|
|
|
}
|
|
|
|
|
} /* Output:
|
|
|
|
|
true
|
|
|
|
|
true
|
|
|
|
|
false
|
|
|
|
|
true
|
|
|
|
|
*///:~
|