The question is simple, but I'm not sure if this is possible ...
if we have a class like
class A { private int foo; public A(int bar) { this.foo = bar; } public A setFoo(int bar) { this.foo = bar; return this; } public int getFoo() { return this.foo; } public void doSomething() { this.foo++; } }
we see that this is just a class with a private member and setter / getter. The interesting thing is that to enable the chain of methods, the setter returns this .
So, we can do such things:
A a = new A(0); a.setFoo(1).doSomething();
The problem here is that when I try to extend this class, add some functions that implement an interface like this
class B extends A implements I { public B(int bar) { this.super(bar); } public void methodI() {
Everything seems to be fine until I start using it like that
B b = new B(1); b.setFoo(2).methodI();
Since setFoo actually returns an instance of A , not an instance of B , but methodI not exist in methodI ...
Any workaround? Thanks.
By the way, I just wrote the basic code just to understand, but if you want to know more, I'm just trying to extend some of the main libgdx classes (for example, Math.Vector2 , Math.Vector3 ) to implement Poolable.
source share