什么是&QUOT的最佳途径;越权QUOT;枚举? [英] What is the best way to "override" enums?

查看:126
本文介绍了什么是&QUOT的最佳途径;越权QUOT;枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
枚举“继承与rdquo;的






我有一些延伸的抽象类的类。抽象父类定义了一组值的枚举。一些子类继承父类的枚举值,但一些子类需要枚举值是不同的。有什么办法以某种方式覆盖这些特定的子类的枚举,如果没有,什么是要达到什么我描述一个好办法?



 类父类
{
私人MyEnum m_EnumVal;
公共虚拟MyEnum EnumVal
{
{返回m_EnumVal; }
集合{m_EnumVal =价值; }
}

公共枚举MyEnum {A,B,C};
}

类ChildClass:父类
{
私人MyEnum m_EnumVal;
公共虚拟MyEnum EnumVal
{
{返回m_EnumVal; }
集合{m_EnumVal =价值; }
}

公共枚举MyEnum {D,E,F};
}


解决方案

其他答案是真实的,如果你不拥有或无法修改原来的基类或枚举。如果可以的话,那么你可以使用类型安全枚举模式即可。它允许你定义自己的枚举类型,而不是枚举派生,你想让他们做的(包括支持继承)不管这事。

 公共类MyEnum 
{
公共静态只读MyEnum A =新MyEnum(A);
公共静态只读MyEnum B =新MyEnum(B);
公共静态只读MyEnum C =新MyEnum(C);

公共重写字符串的ToString()
{
返回值;
}

保护MyEnum(字符串值)
{
THIS.VALUE =价值;
}

公共字符串值{获得;私人集; }
}

公共密封类MyDerivedEnum:MyEnum
{
公共静态只读MyDerivedEnum D =新MyDerivedEnum(D);

私人MyDerivedEnum(字符串值)
:基地(价值)
{
}
}

类节目
{
静态无效的主要(字串[] args)
{
MyEnum等等= MyEnum.A;
的System.Console.WriteLine(等等);
等等= MyDerivedEnum.D;
的System.Console.WriteLine(等等);
}
}




一个结果
D结果
按任意键继续。 。



Possible Duplicate:
Enum “Inheritance”

I have a number of classes which extend an abstract class. The abstract parent class defines an enum with a set of values. Some of the subclasses inherit the parent class's enum values, but some of the subclasses need the enum values to be different. Is there any way to somehow override the enum for these particular subclasses, and if not, what is a good way to achieve what I'm describing?

class ParentClass
{
    private MyEnum m_EnumVal;
    public virtual MyEnum EnumVal
    {
        get { return m_EnumVal; }
        set { m_EnumVal = value; }
    }

    public enum MyEnum { a, b, c };
}

class ChildClass : ParentClass
{
    private MyEnum m_EnumVal;
    public virtual MyEnum EnumVal
    {
        get { return m_EnumVal; }
        set { m_EnumVal = value; }
    }

    public enum MyEnum { d, e, f };
}

解决方案

The other answers are true, if you don't own or can't modify the original base class or enumeration. If you can, then you could use the typesafe enum pattern. It allows you to define your own enum types, not derived from enum, that do whatever you want them to do (including support inheritance).

public class MyEnum
{
  public static readonly MyEnum A = new MyEnum("A");
  public static readonly MyEnum B = new MyEnum("B");
  public static readonly MyEnum C = new MyEnum("C");

  public override string ToString()
  {
    return Value;
  }

  protected MyEnum(string value)
  {
    this.Value = value;
  }

  public string Value { get; private set; }
}

public sealed class MyDerivedEnum : MyEnum
{
  public static readonly MyDerivedEnum D = new MyDerivedEnum("D");

  private MyDerivedEnum(string value)
    : base(value)
  {
  }
}

class Program
{
    static void Main(string[] args)
    {
        MyEnum blah = MyEnum.A;
        System.Console.WriteLine(blah);
        blah = MyDerivedEnum.D;
        System.Console.WriteLine(blah);
    }
}

A
D
Press any key to continue . . .

这篇关于什么是&QUOT的最佳途径;越权QUOT;枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆