java - Overriding a chained method -



java - Overriding a chained method -

let's have class a method useful chaining:

class { foo() { // stuff homecoming this; } }

you a.foo().foo().foo() (think: builder pattern). allow class b extend it:

class b extends { } b b;

now invoking b.foo() returns actual type b, declared a. hence cannot write:

b other = b.foo();

i have write either:

b other = (b) b.foo();

or override foo() in b:

class b extends { @override b foo() { super.foo(); homecoming this; } }

is there nice way this?

did implementing additional generic method as(type) superclass. helps have nicer cast fluent interfaces.

class { public foo() { homecoming this; } @suppresswarnings("unchecked") public <t extends a> t as(class<t> clazz) { homecoming (t) this; } } class b extends {}

so can write a.foo().as(b.class).methodofb(..). , not need reimplement foo() in subclasses.

java

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -