Extending an enum via inheritance

后端 未结 15 2503
温柔的废话
温柔的废话 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: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.

提交回复
热议问题