Extending an enum via inheritance

后端 未结 15 2464
温柔的废话
温柔的废话 2020-11-27 17:40

I know this rather goes against the idea of enums, but is it possible to extend enums in C#/Java? I mean \"extend\" in both the sense of adding new values to an enum, but a

相关标签:
15条回答
  • 2020-11-27 18:07

    You can't inherit from/extend an enum, you can use attributes to declare a description. If you're looking for an integer value, that's built-in.

    0 讨论(0)
  • 2020-11-27 18:11

    I didn't see anyone else mention this but the ordinal value of an enum is important. For example, with grails when you save an enum to the database it uses the ordinal value. If you could somehow extend an enum, what would be the ordinal values of your extensions? If you extended it in multiple places how could you preserve some kind of order to these ordinals? Chaos/instability in the ordinal values would be a bad thing which is probably another reason why the language designers have not touched this.

    Another difficulty if you were the language designer, how can you preserve the functionality of the values() method which is supposed to return all of the enum values. What would you invoke this on and how would it gather up all of the values?

    0 讨论(0)
  • 2020-11-27 18:12

    I would like to be able to add values to C# enumerations which are combinations of existing values. For example (this is what I want to do):

    AnchorStyles is defined as

    public enum AnchorStyles { None = 0, Top = 1, Bottom = 2, Left = 4, Right = 8, }

    and I would like to add an AnchorStyles.BottomRight = Right + Bottom so instead of saying

    my_ctrl.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
    

    I can just say

    my_ctrl.Anchor = AnchorStyles.BottomRight;
    

    This doesn't cause any of the problems that have been mentioned above, so it would be nice if it was possible.

    0 讨论(0)
  • 2020-11-27 18:15

    Enums are supposed to represent the enumeration of all possible values, so extending rather does go against the idea.

    However, what you can do in Java (and presumably C++0x) is have an interface instead of a enum class. Then put you standard values in an enum that implements the feature. Obviously you don't get to use java.util.EnumSet and the like. This is the approach taken in "more NIO features", which should be in JDK7.

    public interface Result {
        String name();
        String toString();
    }
    public enum StandardResults implements Result {
        TRUE, FALSE
    }
    
    
    public enum WTFResults implements Result {
        FILE_NOT_FOUND
    }
    
    0 讨论(0)
  • 2020-11-27 18:15

    As far as java is concerned it is not allowed because adding elements to an enum would effectively create a super class rather than a sub class.

    Consider:

     enum Person (JOHN SAM}   
     enum Student extends Person {HARVEY ROSS}
    

    A general use case of Polymorphism would be

     Person person = Student.ROSS;   //not legal
    

    which is clearly wrong.

    0 讨论(0)
  • 2020-11-27 18:22

    You can use .NET reflection to retrieve the labels and values from an existing enum at run-time (Enum.GetNames() and Enum.GetValues() are the two specific methods you would use) and then use code injection to create a new one with those elements plus some new ones. This seems somewhat analagous to "inheriting from an existing enum".

    0 讨论(0)
提交回复
热议问题