Java class level type inference
I know the code below doesn't compile, and yes I might be crazy. What I'm trying to do is slip an implementation (B below) of a abstract class (A) as the super class of class (C) that also extends the abstract class. I'd also like to defer method override to the super class. Something like...
abstract class A {
abstract Object get();
abstract Object do();
Object action();
}
class B extends A {
B(B obj) {
super(obj.getName()...)
}
Object get() { ... }
Object do() { ... }
}
C<T> extends <T extends A> {
C(<T extends A> obj) {
super(obj)
}
Object do() { ... }
Object whatever() {
return action();
}
}
Where...
C aC = new aC(new B());
aC.g开发者_C百科et() -> B.get()
aC.do() -> C.do()
aC.action() -> A.action()
I think you want to use the delegate pattern.
class C extends A {
private final A delegate;
C(A obj) {
this.delegate = obj;
}
Object get() { return delegate.get() }
Object whatever() {
return action();
}
}
精彩评论