Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #28741
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: use case for extending enum, but this is not possible in java |
| Date | 2014-06-14 18:58 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <lniuk4$bn3$1@dont-email.me> (permalink) |
| References | <lniljr$8h9$1@news.m-online.net> |
On 6/14/2014 4:24 PM, Laura Schmidt wrote:
> public void onListCommand (ListCommand cmd)
> {
> AppListCommand c = (AppListCommand) cmd;
>
> switch (c)
> {
> ...
> }
I have to agree with Marcel Muller here. Enums, the way you use them,
are not polymorphic. And using a switch to emulate polymorphism is
almost always a bad idea. See Replace Conditional with Polymorphism.
http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html
However you can fake it a bit in Java. Java enum are sorta-kinda
polymorphic. (I think that's the technical term.)
interface MyCommand {
Object eval();
}
void onListCommand( MyCommand cmd ) {
System.out.println( cmd.eval() );
}
public enum ListCommand implements MyCommand
{
OPEN,
EDIT,
DELETE;
public Object eval() { return this.ordinal(); }
}
public enum AppListCommand implements MyCommand
{
OPEN,
CLOSE,
SOMETHINGSPECIAL() {
public Object eval() { return "Hi";}};
public Object eval() { return this.ordinal(); }
}
Not compiled or tested, although part was:
public static void main( String[] args )
{
onListCommand( AppListCommand.OPEN, AppListCommand.CLOSE,
AppListCommand.SOMETHINGSPECIAL );
}
private static void onListCommand( MyCommand... cmd )
{
for( MyCommand x : cmd ) {
System.out.println( x.eval() );
}
}
run:
0
1
Hi
BUILD SUCCESSFUL (total time: 1 second)
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
use case for extending enum, but this is not possible in java Laura Schmidt <ls@mailinator.com> - 2014-06-15 01:24 +0200 Re: use case for extending enum, but this is not possible in java Steven Simpson <ss@domain.invalid> - 2014-06-15 01:04 +0100 Re: use case for extending enum, but this is not possible in java Marcel Müller <news.5.maazl@spamgourmet.org> - 2014-06-15 02:53 +0200 Re: use case for extending enum, but this is not possible in java markspace <markspace@nospam.nospam> - 2014-06-14 18:58 -0700 Re: use case for extending enum, but this is not possible in java Robert Klemme <shortcutter@googlemail.com> - 2014-06-15 12:20 +0200 Re: use case for extending enum, but this is not possible in java Robert Klemme <shortcutter@googlemail.com> - 2014-06-15 12:21 +0200 Re: use case for extending enum, but this is not possible in java "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-06-15 11:58 +0100 Re: use case for extending enum, but this is not possible in java Wanja Gayk <brixomatic@yahoo.com> - 2014-06-16 01:08 +0200 Re: use case for extending enum, but this is not possible in java Roedy Green <see_website@mindprod.com.invalid> - 2014-06-15 22:20 -0700
csiph-web