Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #32052
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2015-07-16 17:16 -0700 |
| References | <slrnmq5deh.uoi.avl@login.logic.tuwien.ac.at> |
| Message-ID | <ca4e353c-8152-4426-8d8d-c06a396ba37f@googlegroups.com> (permalink) |
| Subject | Re: anonymous generic class with reflexive type argument... |
| From | Lew <lewbloch@gmail.com> |
On Sunday, July 12, 2015 at 11:45:53 AM UTC-7, Andreas Leitgeb wrote:
> A while ago, there was a thread about a trick (or pattern) involving
> a reflexive type argument:
>
> abstract class Base<T extends Base<T>> {
> T doAction() {
> // do something ...
> return self(); // for chain-calling.
> }
> abstract T self();
> }
>
> class Sub extends Base<Sub> {
> Sub self() { return this; }
> }
>
> So far, that's the pattern (just for the context).
>
> Now my question is: does there exist also a trick to create an
> anonymous subclass directly of Base, and if so, how would that
> look like?
>
> ... = new Base<???>() {
> ??? doAction() {
> // do something more specific
> return self();
> }
> ??? self() { return this; }
> }
>
> Of course I could create some non-anonymous and non-generic helper
> class Foo extends Base<Foo>, implement self() returning Foo, and then
> create an anonymous Subclass of Foo, but is there also a direct way?
You'll have to return the immediate supertype, which is the usual practice with anonymous subtypes.
--
Lew
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
anonymous generic class with reflexive type argument... Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2015-07-12 18:44 +0000
Re: anonymous generic class with reflexive type argument... Lew <lewbloch@gmail.com> - 2015-07-16 17:16 -0700
Re: anonymous generic class with reflexive type argument... Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2015-07-17 11:40 +0000
csiph-web