This is not a question of the new features in enum. This is not a question that waits for an answer that says enum is equivalent to class xyz extends Enum. If you wish to preach on how enums are used, as found in numerous tutorials, please don't answer.
This question is specifically on how to use the class java.lang.Enum. Rather it is true that java.lang.Enum cannot be directly extended nor instantiated, right? 开发者_开发知识库It is true that java.lang.Enum is only for the benefit of enum type and nothing else right?
If so, the variable colours is pretty meaningless, right? Even though the compiler allows it.
final static Enum<colors> colours = null;
enum colors {"R","G","B"};
So, what could I possibly equate the variable colours besides null.
of course, compiler won't allow me to do this
class BaseColour extends Enum<BaseColour>{ ,,,}
but it allows enum an unfair advantage to able to do it as a compiler's behind-the-curtains manipulation.
I am asking this question because I wish to do something like these
class Reddish extends BaseColour {"Crimson", "Blood", "Pink"};
class Purplish extends BaseColour {"Violet", "Evening Sky", "Brinjal"};
class Greenish extends BaseColour {"Algae", "Leaf"};
I wish to pass any of these, and only any of these, enumerated types into a method
void squeezeColour(BaseColour colourConstraint)
{ ... }
Is it possible? /** If not, and if you happen to be James Gosling et al, would you kindly consider this for the next version of Java specs? **/
Further addition to question: I forgot to say this ... What I am working on is using an EnumMap or EnumSet and exploiting the contains and range methods. Is that the best I could do?
You could just use a Dictionary to map an abstract look (reddish) to an Enum. That way you'd keep the Enum and you could compare it using the dictionary methods.
If you want to do this, you will have to do it yourself, without using enum. If you google the old "type-safe enum" pattern you will find a reasonable way.
This is because of the methods in the Enum class, such as values() and ordinal().
If you could add values by extending, then how are they supposed to end up in the values() method of the base class. If you don't want them in the values() method of the base enum, then it breaks the rule that:
X instanceof MyEnum => MyEnum.values() contains X
And should Reddish.ordinal() disagree with BaseColour.ordinal() or agree. How is this supposed to work. It can be solved (as the C++ dispatch table stuff demonstrates) but is almost certainly not worth the effort.
What an enum actually is, internally, is an integer constant. Or at least, the JVM is free to treat it as one, precisely because of the kind of restrictions that are frustrating you. That makes enums efficient, at the cost of breaking object-orientation.
I suggest you revert to the old "type checked enum" pattern that was used before enums had syntax.
class Foo {
// the constants
public static final Foo RHUBARB = new Foo();
public static final Foo CUSTARD = new Foo();
// only this class and subclasses can create constants
protected Foo(){ }
}
精彩评论