Java 涉及幻数的全局常量的最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3931329/
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
Best practice for global constants involving magic numbers
提问by Billworth Vandory
To avoid magic numbers, I always use constants in my code. Back in the old days we used to define constant sets in a methodless interface which has now become an antipattern.
为了避免幻数,我总是在我的代码中使用常量。回到过去,我们过去常常在无方法接口中定义常量集,现在已成为反模式。
I was wondering what are the best practices? I'm talking about global constants. Is an enum the best choice for storing constants in Java?
我想知道什么是最佳实践?我说的是全局常量。枚举是在 Java 中存储常量的最佳选择吗?
采纳答案by Mot
Using interfaces for storing constants is some kind of abusing interfaces.
使用接口来存储常量是某种滥用接口。
But using Enums is not the best way for each situation. Often a plain int
or whatever else constant is sufficient. Defining own class instances ("type-safe enums") are even more flexible, for example:
但是对于每种情况,使用枚举并不是最好的方法。通常一个普通的int
或其他任何常数就足够了。定义自己的类实例(“类型安全枚举”)更加灵活,例如:
public abstract class MyConst {
public static final MyConst FOO = new MyConst("foo") {
public void doSomething() {
...
}
};
public static final MyConst BAR = new MyConst("bar") {
public void doSomething() {
...
}
};
protected abstract void doSomething();
private final String id;
private MyConst(String id) {
this.id = id;
}
public String toString() {
return id;
}
...
}
回答by Boris Pavlovi?
Yes it is. Enum is the best choice.
是的。枚举是最好的选择。
You are getting for free:
您将免费获得:
- constants
- immutable objects
- singletons
- 常数
- 不可变对象
- 单身人士
All in one.
一体。
But wait, there's some more. Every enum value can have its own fields and methods. It's a rich constant object with behavior that allows transformation into different forms. Not only toString, but toInt, toWhateverDestination you need.
但是等等,还有一些。每个枚举值都可以有自己的字段和方法。它是一个丰富的常量对象,其行为允许转换为不同的形式。不仅 toString,而且 toInt,toWhateverDestination 你需要。
回答by nanda
Enum is best for most of the case, but not everything. Some might be better put like before, that is in a special class with public static constants.
Enum 最适合大多数情况,但不是所有情况。有些可能像以前一样更好地放置,即在具有公共静态常量的特殊类中。
Example where enum is not the best solution is for mathematical constants, like PI. Creating an enum for that will make the code worse.
枚举不是最佳解决方案的示例是数学常数,例如 PI。为此创建一个枚举会使代码变得更糟。
enum MathConstants {
PI(3.14);
double a;
MathConstants(double a) {
this.a = a;
}
double getValueA() {
return a;
}
}
Usage:
用法:
MathConstants.PI.getValueA();
Ugly isn't it? Compare to:
丑是不是?相比于:
MathConstants.PI;
回答by willcodejavaforfood
For magic numbers where the number actual has a meaning and is not just a label you obviously should not use enums. Then the old style is still the best.
对于实际数字具有含义并且不仅仅是标签的幻数,您显然不应该使用枚举。那么旧的风格仍然是最好的。
public static final int PAGE_SIZE = 300;
When you are just labelling something you would use an enum.
当您只是标记某些东西时,您将使用枚举。
enum Drink_Size
{
TALL,
GRANDE,
VENTI;
}
Sometimes it makes sense to put all your global constants in their own class, but I prefer to put them in the class that they are most closely tied to. That is not always easy to determine, but at the end of the day the most important thing is that your code works :)
有时将所有全局常量放在它们自己的类中是有意义的,但我更喜欢将它们放在与它们最密切相关的类中。这并不总是很容易确定,但归根结底,最重要的是您的代码可以正常工作:)
回答by Mikhail Berastau
Forget about enums - now, when static import is available in Java, put all your constants in REAL class (instead of interface) and then just import the static members from all the other ones using import static <Package or Class>
.
忘记枚举 - 现在,当静态导入在 Java 中可用时,将所有常量放在 REAL 类(而不是接口)中,然后使用import static <Package or Class>
.