Java:方法中的枚举参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/142420/
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
Java: Enum parameter in method
提问by Jean Paul Galea
I have a method lets say:
我有一个方法可以说:
private static String drawCellValue(
int maxCellLength, String cellValue, String align) { }
and as you can notice, I have a parameter called align. Inside this method I'm going to have some if condition on whether the value is a 'left' or 'right'.. setting the parameter as String, obviously I can pass any string value.. I would like to know if it's possible to have an Enum value as a method parameter, and if so, how?
正如您所注意到的,我有一个名为 align 的参数。在这个方法中,我将有一些关于值是“左”还是“右”的 if 条件..将参数设置为字符串,显然我可以传递任何字符串值..我想知道这是否可能有一个枚举值作为方法参数,如果是这样,如何?
Just in case someone thinks about this; I thought about using a Boolean value but I don't really fancy it. First, how to associate true/false with left/right ? (Ok, I can use comments but I still find it dirty) and secondly, I might decide to add a new value, like 'justify', so if I have more than 2 possible values, Boolean type is definitely not possible to use.
以防万一有人想到这一点;我想过使用布尔值,但我并不喜欢它。首先,如何将 true/false 与 left/right 关联起来?(好吧,我可以使用注释,但我仍然觉得它很脏)其次,我可能决定添加一个新值,例如“justify”,因此如果我有 2 个以上的可能值,则绝对无法使用布尔类型。
Any ideas?
有任何想法吗?
采纳答案by Carra
This should do it:
这应该这样做:
private enum Alignment { LEFT, RIGHT };
String drawCellValue (int maxCellLength, String cellValue, Alignment align){
if (align == Alignment.LEFT)
{
//Process it...
}
}
回答by Michael Bobick
Sure, you could use an enum. Would something like the following work?
当然,您可以使用枚举。会像下面这样工作吗?
enum Alignment {
LEFT,
RIGHT
}
private static String drawCellValue(int maxCellLength, String cellValue, Alignment alignment) { }
私有静态字符串 drawCellValue(int maxCellLength, String cellValue, Alignment 对齐) { }
If you wanted to use a boolean, you could rename the align parameter to something like alignLeft. I agree that this implementation is not as clean, but if you don't anticipate a lot of changes and this is not a public interface, it might be a good choice.
如果您想使用布尔值,您可以将 align 参数重命名为 alignLeft 之类的名称。我同意这个实现不是那么干净,但如果你不期望有很多变化并且这不是一个公共接口,它可能是一个不错的选择。
回答by Tom
You could also reuse SwingConstants.{LEFT,RIGHT}. They are not enums, but they do already exist and are used in many places.
您还可以重用 SwingConstants.{LEFT,RIGHT}。它们不是枚举,但它们确实已经存在并在许多地方使用。
回答by Joshua DeWald
Even cooler with enums you can use switch:
更酷的枚举你可以使用 switch:
switch (align) {
case LEFT: {
// do stuff
break;
}
case RIGHT: {
// do stuff
break;
}
default: { //added TOP_RIGHT but forgot about it?
throw new IllegalArgumentException("Can't yet handle " + align);
}
}
Enums are cool because the output of the exception will be the name of the enum value, rather than some arbitrary int value.
枚举很酷,因为异常的输出将是枚举值的名称,而不是一些任意的 int 值。
回答by Zamir
I like this a lot better. reduces the if/switch, just do.
我更喜欢这个。减少if/switch,就做。
private enum Alignment { LEFT, RIGHT;
void process() {
//Process it...
}
};
String drawCellValue (int maxCellLength, String cellValue, Alignment align){
align.process();
}
of course, it can be:
当然,它可以是:
String process(...) {
//Process it...
}
回答by Rodney P. Barbati
I am not too sure I would go and use an enum as a full fledged class - this is an object oriented language, and one of the most basic tenets of object orientation is that a class should do one thing and do it well.
我不太确定我会去使用枚举作为一个完整的类 - 这是一种面向对象的语言,面向对象的最基本原则之一是一个类应该做一件事并且做得很好。
An enum is doing a pretty good job at being an enum, and a class is doing a good job as a class. Mixing the two I have a feeling will get you into trouble - for example, you can't pass an instance of an enum as a parameter to a method, primarily because you can't create an instance of an enum.
枚举在枚举方面做得很好,而类作为一个类做得很好。混合这两者我有一种感觉会给你带来麻烦——例如,你不能将一个枚举的实例作为参数传递给一个方法,主要是因为你不能创建一个枚举的实例。
So, even though you might be able to enum.process() does not mean that you should.
所以,即使你可能能够 enum.process() 并不意味着你应该。
回答by BobbyMacBob
You can use an enum in said parameters like this:
您可以在上述参数中使用枚举,如下所示:
public enum Alignment { LEFT, RIGHT }
private static String drawCellValue(
int maxCellLength, String cellValue, Alignment align) {}
then you can use either a switch or if statement to actually do something with said parameter.
那么您可以使用 switch 或 if 语句对所述参数进行实际操作。
switch(align) {
case LEFT: //something
case RIGHT: //something
default: //something
}
if(align == Alignment.RIGHT) { /*code*/}