2015-09-07 11:44:36 -06:00
|
|
|
// generics/CreatorGeneric.java
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
abstract class GenericWithCreate<T> {
|
|
|
|
final T element;
|
|
|
|
GenericWithCreate() { element = create(); }
|
|
|
|
abstract T create();
|
|
|
|
}
|
|
|
|
|
|
|
|
class X {}
|
|
|
|
|
|
|
|
class Creator extends GenericWithCreate<X> {
|
|
|
|
@Override
|
|
|
|
X create() { return new X(); }
|
|
|
|
void f() {
|
|
|
|
System.out.println(element.getClass().getSimpleName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class CreatorGeneric {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Creator c = new Creator();
|
|
|
|
c.f();
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
X
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|