接口与枚举

发布于 2024-08-26 23:21:24 字数 33 浏览 5 评论 0原文

接口和枚举之间,哪个更适合声明常量?为什么会这样呢?

Between interfaces and enums, which is better for declaring constants? Why is it so?

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

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

发布评论

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

评论(5

陪你到最终 2024-09-02 23:21:24

使用枚举来声明常量总是更好,因为接口的目标处于完全不同的级别。是的,有很多接口都有 public static final 常量,但我觉得枚举的唯一工作就是为您提供这些常量。

Its always better to use Enums to declare constants as the objective of interfaces are on a totally different level. Yes, there are lots of interfaces which have a public static final constants, but I feel that enums exclusive job is to provide you these constants.

孤单情人 2024-09-02 23:21:24

如果您的常量有特定的类型,如果它们需要某种行为(即方法),或者如果它们是其他值的组合,那么enum就是正确的选择。

例如,假设您正在实现一款纸牌游戏,并且想要表示数值和花色:

enum Rank { 
    ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, 
    EIGHT, NINE, TEN, JACK, QUEEN, KING; 
}
enum Suit { SPADES, CLUBS, DIAMONDS, HEARTS } 

现在不可能创建具有虚假花色或等级的纸牌。

但有时,您只是对在某处声明一堆常用值感兴趣。在这种情况下,将它们放入枚举中只是不必要的工作,因为这些常量只是一种工具,可以帮助我们在计算圆的周长时免于记住 π 等所有小数。圈子什么的。哪个看起来更好?

// Using enum:
enum MathConstant { 
    PI(3.14159265358979323846), E(2.7182818284590452354);
    private final double value;
    MathConstant(double v) { value = v; }
    public double value() { return value; } 
}
// Usage:
double circumference = MathConstant.PI.value() * diameter;

// Using a constant class:
final class MathConstants { 
    private MathConstants() { throw new UnsupportedOperationException(); }
    public static final double PI = 3.14159265358979323846,
                               E = 2.7182818284590452354;
}
// Usage:
double circumference = MathConstants.PI * diameter;

至于接口:永远不要将常量放入接口中。 “常量接口”模式不好(理由),自从 import static 添加到 Java 以来,使用它的唯一参数已变得无效。

If there is a reason for your constants to have a specific type, if they need some kind of behavior (i.e., methods), or if they are composites of other values, enums are the way to go.

For example, let's assume you're implementing a card game and you want to represent values and suits:

enum Rank { 
    ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, 
    EIGHT, NINE, TEN, JACK, QUEEN, KING; 
}
enum Suit { SPADES, CLUBS, DIAMONDS, HEARTS } 

There, it's now impossible to create cards with bogus suits or ranks.

Sometimes, though, you are just interested in having a bunch of frequently used values declared somewhere. In that case, putting them in an enum would just be unnecessary effort, since these constants are just a tool to save us from remembering all the decimals of, say, π when we are calculating the circumference of a circle, or something. Which looks better?

// Using enum:
enum MathConstant { 
    PI(3.14159265358979323846), E(2.7182818284590452354);
    private final double value;
    MathConstant(double v) { value = v; }
    public double value() { return value; } 
}
// Usage:
double circumference = MathConstant.PI.value() * diameter;

// Using a constant class:
final class MathConstants { 
    private MathConstants() { throw new UnsupportedOperationException(); }
    public static final double PI = 3.14159265358979323846,
                               E = 2.7182818284590452354;
}
// Usage:
double circumference = MathConstants.PI * diameter;

As for interfaces: Never put constants in an interface. The "constant interface" pattern is bad (justification), and the only argument to use it has been rendered invalid since import static was added to Java.

疑心病 2024-09-02 23:21:24

接口旨在定义通用行为,枚举用于定义通用值。

枚举代​​表一个真实值,可以与另一个值进行比较,或轻松存储在数据库中。
您还可以有一个标志枚举(在 C# 中,在 Java 中不知道),它允许您对枚举的值执行二进制运算(AND、OR、XOR 等)。

Interfaces are designed to define common behaviours, enums to define common values.

Enum represents a real value which can be compared to another value, or stored in the database easily.
You can also have a flag-enum (in C#, don't know in Java) which let you perform binary operations on enum's values (AND, OR, XOR, etc).

梦行七里 2024-09-02 23:21:24

如果您使用 Java 5 或更高版本,那么 Enum 是最佳选择。唯一的例外是您的 const 列表是开放的并且可以扩展。枚举不能扩展。另一个例外是它是单个值(例如 MAX_INTEGER)。

If you work with Java 5 or newer then Enum are the way to go. The only exception is if your list of const is open and can be extended. Enums can not be extended. Another exception is if it are single values like a MAX_INTEGER.

一身仙ぐ女味 2024-09-02 23:21:24

只是一个代码:

public interface InterfaceForConstants() {
    String **TOTO** = "Et voilà !";
    double **PI** = 3.14159265358979323846;
}

使用:

class ClasseName implements InterfaceForConstants () {

    String myString;

    if (myString.equals(**TOTO**) {
        // do something
    }

    double circumference = **PI** * diameter; // instead of MathConstants.PI * diameter;

}

干净简单。如果某样东西可以满足您的需要,那就选择最简单的!
按规则编写代码很好,编写易于阅读的代码更好,即使不遵守规则。考虑一下几个月后您何时必须阅读代码。
如果你想更具体的话,制作一个javadoc来解释它!

Just a code :

public interface InterfaceForConstants() {
    String **TOTO** = "Et voilà !";
    double **PI** = 3.14159265358979323846;
}

Using :

class ClasseName implements InterfaceForConstants () {

    String myString;

    if (myString.equals(**TOTO**) {
        // do something
    }

    double circumference = **PI** * diameter; // instead of MathConstants.PI * diameter;

}

Clean and simple. If something can do what you need, take the simplest one!
Writing code by the rules is good, writing code that is easy to read is better, even if a rule is not respected. Think about when you have to read your code after a few months.
If you want to be more specific, make a javadoc to explain it!

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