java 接口与枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2528561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:37:17  来源:igfitidea点击:

Interfaces vs. enums

javaenums

提问by abson

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

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

采纳答案by bragboy

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 finalconstants, but I feel that enums exclusive job is to provide you these constants.

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

回答by gustafc

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.

如果您的常量有特定类型的原因,如果它们需要某种行为(即方法),或者如果它们是其他值的组合,enums 就是要走的路。

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, Hyman, 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 enumwould 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?

但是,有时,您只是对在某处声明一堆常用值感兴趣。在这种情况下,将它们放入一个enum只是不必要的努力,因为这些常数只是一种工具,可以让我们在计算圆的周长或其他东西时避免记住所有小数,例如 π。哪个更好看?

// 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 staticwas added to Java.

至于接口:永远不要在接口中放置常量。“常量接口”模式是不好的(justification),自从import static被添加到 Java以来,使用它的唯一参数已经无效。

回答by twk

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).

Enum 代表一个真实的值,可以与另一个值进行比较,或者很容易地存储在数据库中。您还可以使用标志枚举(在 C# 中,在 Java 中不知道),它允许您对枚举的值(AND、OR、XOR 等)执行二进制操作。

回答by Horcrux7

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.

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