如何改进这个内部枚举代码?

发布于 2024-10-12 05:22:08 字数 526 浏览 2 评论 0原文

我有这样的构造:

public class Constants {
 enum SystemA implements Marker {
    ConstOne(1), ConstTwo(2), ConstThree(3);
    SystemA(int i)
   {
      number = i;
   }
   int number;
 }

  enum SystemB implements Marker {
    ConstFour(4), ConstFive(5), ConstSix(6);
  SystemB(int i)
   {
      number =i;
   } 
   int number;
 }
}

我有 Marker 所以我可以传递给这样的方法: method(Constants.SystemA)method(Constants.SystemB)

列出所有枚举值的最佳方法是什么?我还想确保它不会重复任何枚举中的数字。

I have this construct:

public class Constants {
 enum SystemA implements Marker {
    ConstOne(1), ConstTwo(2), ConstThree(3);
    SystemA(int i)
   {
      number = i;
   }
   int number;
 }

  enum SystemB implements Marker {
    ConstFour(4), ConstFive(5), ConstSix(6);
  SystemB(int i)
   {
      number =i;
   } 
   int number;
 }
}

I have Marker so I can pass to method like this:
method(Constants.SystemA) or method(Constants.SystemB)

What is the best way to list all the enum values? I also want to make sure that it is not duplicating the number in any of the enums.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

姜生凉生 2024-10-19 05:22:08

如果两个枚举的约定相同,则可以将其合并为一个枚举。
但是,为了区分这两个系统,您可以有一个单独的“SystemType”枚举,其中包含 A、B...,并且您可以为 System 枚举中的每个常量分配一个 SystemType。

此外,您的 SystemType 枚举可以提供一个实例方法,该方法将返回给定类型的所有系统,以便您可以根据需要仅迭代该子集。

至于编号,如果序列像您的示例中一样简单,您可以使用枚举常量的 ordinal() 方法,以便您只需依赖常量声明顺序(可能添加一些数学)。我的意思是:

public enum SystemType {
    A, B;

    public Set<System> getSystems() {
        Set<System> systems = EnumSet.noneOf(System.class);
        for (System system : System.values()) {
            if (system.getType() == this) {
                systems.add(system);
            }
        }
        // you could also store this result
        // as an instance variable for caching.
        return systems; 
    }
}

public enum System {
    ConstOne(SystemType.A),
    ConstTwo(SystemType.A),
    ConstThree(SystemType.A),
    ConstFour(SystemType.B),
    ConstantFive(SystemType.B);

    private final SystemType systemType;

    private System(final SystemType systemType) {
        this.systemType = systemType;
    }

    public int getNumber() {
        return ordinal() + 1;
    }

    public SystemType getType() {
        return systemType;
    }

}

如果您确实需要为不同的系统类型定义不同的契约(仅对 SystemX 或其他有意义的方法),这当然是无用的。

附:
当然,System 是一个非常糟糕的名字,因为它隐藏了 java.lang.System 类。

If the contract of the two enums is the same, you can have it into one single enum.
However, in order to distinguish the two systems, you can have a separate "SystemType" enum that will contain A, B, ... and you can assign a SystemType to each constant in your System enum.

Furthermore, your SystemType enum can provide an instance method that will return all the systems for a given type, so that you can iterate through only that subset as needed.

As for the numbering, if the sequence is as simple as in your example, you can make use of the ordinal() method of the enum constants so that you'll simply rely on the constant declaration order (perhaps with some math added). Here's what I mean:

public enum SystemType {
    A, B;

    public Set<System> getSystems() {
        Set<System> systems = EnumSet.noneOf(System.class);
        for (System system : System.values()) {
            if (system.getType() == this) {
                systems.add(system);
            }
        }
        // you could also store this result
        // as an instance variable for caching.
        return systems; 
    }
}

public enum System {
    ConstOne(SystemType.A),
    ConstTwo(SystemType.A),
    ConstThree(SystemType.A),
    ConstFour(SystemType.B),
    ConstantFive(SystemType.B);

    private final SystemType systemType;

    private System(final SystemType systemType) {
        this.systemType = systemType;
    }

    public int getNumber() {
        return ordinal() + 1;
    }

    public SystemType getType() {
        return systemType;
    }

}

This is of course useless if you really need to define different contracts for the different system types (methods that only make sense for SystemX or whatever).

P.S.:
Of course, System is a really bad name, as it hides the java.lang.System class.

泪眸﹌ 2024-10-19 05:22:08

由于您在代码中声明所有枚举,因此您有责任提供不重叠的数字(如果需要)。
更有趣的问题是:为什么需要这个?听起来好像您正在将枚举用于它们并非设计用途的东西。

Since you are declaring all enums in code, it is your responsibility to provide non-overlapping numbers, if you need this.
The more interesting question is: Why would you need this? It smells like you are using enums for things they weren't designed for.

桃酥萝莉 2024-10-19 05:22:08

我将依赖一个类 - 一个工厂类 - 来创建和控制系统类的实例。

I would rely on one class - a factory class - to create and control the instances of your System classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文