当成员与另一个类的公共变量相同时,使用枚举 [英] Use of Enum when members are the same as another class's public variables

查看:189
本文介绍了当成员与另一个类的公共变量相同时,使用枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我觉得我做错了,我怀疑有更有效的方式来做事情。

I have a situation where I feel like I'm doing something wrong, and I suspect there's a MUCH more efficient way to do things.

我有一个类有一些变量:

I have a class with some variables:

public class Structure  {
    public float size;
    public float pos_x;
    public float pos_y;
    public float pos_z;
}

我有一个这些成员的枚举与上述完全相同字段:

And I have an enum with these members which are exactly the same as the above fields:

public enum StructureProperty {
    SIZE,
    POS_X,
    POS_Y,
    POS_Z,
}

我有AnotherClass需要存储一个StructureProperty,其相关价值。

I have AnotherClass that needs to store a single StructureProperty, and its associated value.

public class AnotherClass {
    public StructureProperty property;
    public float value;
    public float ID;
    public float parentID;
    ..some methods and things
}

我有一个方法,抓住这个其他类,并且看看要使用枚举更新哪个属性:

I have a method that grabs this other class, and looks to see which property to update using the enum:

void UpdateStructure (AnotherClass anotherClassInstance) {
    switch (anotherClassInstance.property) {

    case StructureProperty.SIZE:
        Structure.size += anotherClassInstance.value;
        break;

    ... for all other cases 
    but the logic might be different 
    (not always +=, sometimes several lines)

}

此方法需要检查以查看要在结构中更新的属性,然后进行更新。我觉得我可能会打破某种面向对象的方法,因为我需要维护一个枚举列表和一个变量列表在结构。请注意,我预见在构建我的程序时经常添加和删除属性。希望有人可以通过正确的方式引导我,使代码更加面向未来。

This method needs to check to see which property to update in the structure, and then update it. I feel like I might be breaking some kind of object oriented approach here since I need to maintain both a list of enums and a list of variables in Structure. Note that I foresee frequently adding and removing properties as I build my program. Hopefully someone can steer me in the right way of making this code more future-proof.

我在c#中编程,但我认为这个问题适用于所有主要的OO编程语言

I'm programming in c#, but I think this question applies to all major OO programming languages.

谢谢。

推荐答案

Java的立场。我确定c#将有类似的结构。我没有看到需要维护一个Structure类和一个StructureProperty枚举。假设这些属性需要在运行时修改,并且没有将它们存储在属性文件中的用例,我认为Structure和StructureProperty可以合并到一个枚举中。

I am answering this from a Java standpoint. I am sure c# will have similar constructs. I don't see any necessity of maintaining a Structure class and a StructureProperty enum. Assuming these properties need to be modified at runtime, and there is no use case of storing them in a properties file, I think Structure and StructureProperty can be consolidated into one Enum.

以下是您可以如何思考:

Here's how you could think in that direction :

public class StructurePropertyExample {

private static enum StructureProperty {

    SIZE(0.0f), POS_X(0.0f), POS_Y(0.0f), POS_Z(0.0f);

    private float value;

    private StructureProperty(float value) {
        this.value = value;
    }

    public float getValue() {
        return value;
    }

    public void setValue(float value) {
        this.value = value;
    }
};

public static void main(String[] args) {

    float newValue = 1.23f;

    for (StructureProperty property : StructureProperty.values()) {
        switch (property) {
            case SIZE: property.setValue(newValue);
                       break;
            case POS_X : // similar as above
            case POS_Y :
            case POS_Z : 
        }
    }

}

}

如果我们在谈论不同的数据类型,可以将枚举更改为保存包装对象,如下所示:

In case we are talking about different data types, the enum can be changed to hold wrapped Objects as below :

    private static enum StructureProperty {

    SIZE, POS_X, POS_Y, POS_Z;

    private Object value;

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }
};

这篇关于当成员与另一个类的公共变量相同时,使用枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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