简体   繁体   English

C#覆盖枚举

[英]c# overriding enum

I know that maybe this question has been asked before, but I can't seem to find a proper solution (having in mind that I am not a C# expert but a medium level user)... 我知道也许以前有人问过这个问题,但是我似乎找不到合适的解决方案(请记住,我不是C#专家,而是中级用户)...

I prepared a base class including an enum (AnimationStates) for animation states that my screen objects might have (for example a Soldier might have different states whereas a bird could have another set of states..) .. The base class is serving the purpose of storing update methods and other things for my animated screen objects (like animating all of them in the same manner)... The enum in the base class is (naturally) empty inside.. I have methods written utilizing the enum members... 我准备了一个基类,其中包括一个枚举(AnimationStates),用于我的屏幕对象可能具有的动画状态(例如,士兵可能具有不同的状态,而鸟可能具有另一组状态。..)为我的动画屏幕对象存储更新方法和其他内容的方式(例如以相同的方式对它们进行动画处理)...基类中的枚举在内部(自然)为空。.我有一些使用枚举成员编写的方法。 。

If I define enum in my child classes by "public new enum...", I can "inherit" it.. right ? 如果我通过“ public new enum ...”在子类中定义枚举,则可以“继承”它。 Moreover, and interestingly, I have a Dictionary in base class and I am trying to pass it from a child (ie soldier, or bird) to its base (animatedobject) class ... But I can't.. 而且,有趣的是,我在基类中有一个Dictionary,并且试图将它从孩子(即士兵或鸟类)传递到其基类(动画对象)……但是我不能。

I feel I am doing something wrong or missing.. Any ideas ? 我觉得我做错了什么或想念什么。有什么想法吗?

Well, you cannot do it directly with enums in C#. 好吧,您不能使用C#中的枚举直接执行此操作。

I would propose taking more object-oriented approach, and replace the enums with real objects. 我建议采取更多的面向对象的方法,并用真实的对象代替枚举。 This way you define an interface IAnimationState in your base class, and add an abstract method getAnimationState() as well. 这样,您可以在基类中定义接口IAnimationState ,并添加抽象方法getAnimationState() In the screen object classes you just implement this method, returning some specific implementation of the interface IAnimationState . 在屏幕对象类中,您只需实现此方法,即可返回接口IAnimationState某些特定实现。 This way you can put some logic into the small animation state classes, making your project more modular. 这样,您可以将一些逻辑放入小型动画状态类中,从而使您的项目更具模块化。

You can't expand enums. 您不能扩展枚举。 You can create new enums in derived classes but they're distinct. 您可以在派生类中创建新的枚举,但是它们是不同的。

I think you should just use int constants. 我认为您应该只使用int常量。

An enumerated type represents a simple set of values. 枚举类型表示一组简单的值。 That's it. 而已。 You are trying to use an enum as a class type when it simply doesn't fit the bill. 您正试图将枚举作为类类型使用,因为它根本不符合要求。

Just create an enum (if you actually need to) and make a "real" type for the complex operations. 只需创建一个枚举(如果确实需要),然后为复杂的操作创建一个“真实”类型。

enum SomeEnum { Foo, Bar }

class Whatever
{
    public SomeEnum { get { return SomeEnum.Foo; } }
}

This question is a good example of developing a solution without really understanding the problem. 这个问题是在没有真正理解问题的情况开发解决方案的一个很好的例子 Instead of proposing your solution and asking us to figure out the last 20% that doesn't make any sense, tell us what you are actually trying to accomplish. 与其提出建议的解决方案,而不是要求我们找出没有任何意义的最后20%,请告诉我们您实际上想要实现的目标。 Someone here may have a better approach that you haven't even thought of. 这里的某个人可能有一个您甚至没有想到的更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM