开发者

Is it possible to extend Java Enums?

Here's what I am looking to accomplish, I have a class that has an enum of some values and I want to subclass that and add more v开发者_Go百科alues to the enum. This is a bad example, but:

public class Digits
{
 public enum Digit
 {
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9
 }
}

public class HexDigits extends Digits
{
 public enum Digit
 {
  A, B, C, D, E, F
 }
}

so that HexDigits.Digit contains all Hex Digits. Is that possible?


No it's not possible. The best you can do is make two enums implement and interface and then use that interface instead of the enum. So:

interface Digit {
  int getValue();
}

enum Decimal implements Digit {
  ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE;

  private final int value;

  Decimal() {
    value = ordinal();
  }

  @Override
  public int getValue() {
    return value;
  }
}

enum Hex implements Digit {
  A, B, C, D, E, F;

  private final int value;

  Hex() {
    value = 10 + ordinal();
  }

  @Override
  public int getValue() {
    return value;
  }
}


No.

Enums cannot be subclassed.

The reasoning here is that an enumeration defines a fixed number of values. Subclassing would break this.


No, you can't. If you look at the definition of Enum, instances of it are final and can't be extended. This makes sense if you understand enums as a finite, final set of values.

There is a difference between a numeral (an syntactic artifact) which could be binary, decimal, hex or whatever, and the actual semantic number, the numerical entity represented syntactically by a numeral in a context (the base system.)

In your example, what you need is

  • enums specifying syntatic numerals (decimal digits and alphabetic symbols representing legal hexadecimals; that is, enum tokens, and
  • classes specifying behavior (or grammar) required for syntactically representing a number as a numeral (using the numeral [syntactic] enums).

That is, you have tokens or symbols and a grammar/behavior that indicate whether a stream of tokens represent a number under a given base.

But that's a bit off the tangent (and as you said, it was just an example for example's sake). Going back to extending enums...

... yo can't and you shouldn't. Enums are not meant to represent things that can be extended. They are for representing a constant set of constant values. There are such things that are un-inheritable.

Also, don't fall into the trap of extending for extension's sake or for trying to force an structure into your code or model.

It might seem to make sense to make one set of values an extension of another. More often than not, it is not. Use inheritance for reusing behavior or complex structure, not just data that has little to no structure with non-reusable behavior associated to it.


It's not possible. And there is a reason. Imagine you could extend an enum:

enum A { ONE, TWO }
enum B extends A { THREE }  // for a total { ONE, TWO, THREE }

Now you can do this:

B b = B.THREE
A a = b  // a = B.THREE ?

But B.THREE is not a valid option for A.

Of course you could make them not-polymorphic, but then it's not really extending.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜