dark9spring 2021-10-21 12:42 采纳率: 55.6%
浏览 14
已结题

怎样给一个继承后的枚举添加成员?

类内或接口内定义的枚举继承后全部没办法再添加成员,除去特性和反射,还有什么好的方法么?

  • 写回答

1条回答 默认 最新

  • XingyuZhao-MSFT 2021-10-22 10:07
    关注

    如果是这种情况,为什么不使用类呢?
    像这样:

    public class Base
    {
        public const int A = 1;
        public const int B = 2;
        public const int C = 3;
    }
    public class Child : Base
    {
        public const int D = 4;
        public const int E = 5;
    }
    

    或者

    public class MyBaseEnum
    {
        public static readonly MyBaseEnum A = new MyBaseEnum( 1 );
        public static readonly MyBaseEnum B = new MyBaseEnum( 2 );
        public static readonly MyBaseEnum C = new MyBaseEnum( 3 );
    
        public int InternalValue { get; protected set; }
    
        protected MyBaseEnum( int internalValue )
        {
            this.InternalValue = internalValue;
        }
    }
    
    public class MyEnum : MyBaseEnum
    {
        public static readonly MyEnum D = new MyEnum( 4 );
        public static readonly MyEnum E = new MyEnum( 5 );
    
        protected MyEnum( int internalValue ) : base( internalValue )
        {
            // Nothing
        }
    }
    

    另外,你不应该在类或接口中定义枚举。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月26日
  • 创建了问题 10月21日

悬赏问题

  • ¥15 rt-thread线程切换的问题
  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊