Java 枚举静态或非静态方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19446204/
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
Enum static or non-static method
提问by none none
Consider the following enum class
考虑以下枚举类
public enum ClassA {
CHECK1("X", 0),
CHECK2("Y", 2),
CHECK3("Z", 1);
private final String id;
private final String cdValue;
private ClsA(String id, String cdValue) {
this.id = id;
this.cdValue = cdValue;
}
private String getId() {
return id;
}
private String getCdValue() {
return cdValue ;
}
private static final List<String> cdValues = new ArrayList<String>();
static {
for (ClassA clsA : ClassA.values()) {
cdValues.add(clsA.getCdValue());
}
}
public boolean isCdValue(String cdValue)
{
if clsValues.contains(cdValue)
return true;
else return false;
}
}
The question that I have is does the method isCdValue
has to be static. I have to use this method isCdValue
for every input given by the client. Therefore the method parameter cdValue
changes for every input.
我的问题是该方法isCdValue
是否必须是静态的。isCdValue
对于客户端给出的每个输入,我都必须使用此方法。因此cdValue
,每个输入的方法参数都会改变。
If it cannot be static then I would like to know how I can access this method. Please note I am primarily interested in learning about static of non-static method call. If it is a non-static call in a enum then how can we call this non static method. I am not trying to resolve the issue of how to get about checking the cdValue
exists or not. It is just an example.
如果它不能是静态的,那么我想知道如何访问此方法。请注意,我主要对学习非静态方法调用的静态感兴趣。如果它是枚举中的非静态调用,那么我们如何调用这个非静态方法。我不是要解决如何检查是否cdValue
存在的问题。这只是一个例子。
回答by Miquel
If you want to access it from ClsA
, you will have to make it static, if you want to access it from an instanceof ClsSa
then it doesn't.
如果要从 访问它ClsA
,则必须将其设为静态,如果要从 的实例访问它,ClsSa
则不能。
A couple of other things: where do you declare clsValues in the first place?
其他几件事:你首先在哪里声明 clsValues ?
There's no need for the complex if, you may replace this:
不需要复杂的 if,你可以替换这个:
public boolean isCdValue(String cdValue)
{
if clsValues.contains(cdValue)
return true;
else return false;
}
with this
有了这个
public boolean isCdValue(String cdValue){
return clsValues.contains(cdValue)
}
Last little thing, I'd strongly suggest you put curly braces around all your if
and else
's clauses, I've spent many a debugging hour because someone added a second line under the else, fooled by the indent and thinking it would only execute on the else.
最后一点,我强烈建议你在所有if
andelse
子句周围加上花括号,我花了很多时间调试,因为有人在 else 下添加了第二行,被缩进愚弄并认为它只会执行别的。
回答by Kent
If you have to put the checking method in the Enum, I think it should be static
如果非要把检查方法放在Enum里,我觉得应该是静态的
you can do this check:
你可以做这个检查:
ClassA.isCdValue(para)
Note that, you cannot new
an Enum object. So if the method in your Enum, and it is not static, you cannot call it unless you have an Instance. but the goal of your method is checking if the string could be an instance.
请注意,您不能new
使用 Enum 对象。所以如果你的 Enum 中的方法不是静态的,除非你有一个实例,否则你不能调用它。但是你的方法的目标是检查字符串是否可以是一个实例。
another possibility is, use an immutable collection in your Enumm, and make it static
and public
. Then you could just call ClassA.CD_VALUES.contains(para)
另一种可能性是,在你的 Enumm 中使用一个不可变的集合,并将它static
和public
. 然后你可以打电话ClassA.CD_VALUES.contains(para)
回答by nullptr
You can use something like this, you do not need static List
but the method has to be static as answered by Kent,
你可以使用这样的东西,你不需要,static List
但该方法必须是静态的,正如 Kent 所回答的那样,
public static ClassA getClassAByCDValue(String cdValue)
{
for(ClassA value: ClassA.values())
{
if(value.cdValue.contains(cdValue))
{
return value;
}
}
return null;
}
public static boolean isCDValue(String cdValue)
{
for(ClassA value: ClassA.values())
{
if(value.cdValue.contains(cdValue))
{
return true;
}
}
return false;
}
Using above will be more appropriate as you just have to take care with adding/removing items in enum.
使用上面会更合适,因为您只需要注意在枚举中添加/删除项目。
回答by Ritesh Gune
does the method isCdValue has to be static.
方法 isCdValue 是否必须是静态的。
Yes, the method isCdValue has to be static here.
An enum is a special kind of class. An enum constant defines an instance of the enum type. An enum type has no instances other than those defined by its enum constants. Hence new
can not be used to instantiate an enum.
是的,方法 isCdValue 在这里必须是静态的。枚举是一种特殊的类。枚举常量定义了枚举类型的一个实例。除了由其枚举常量定义的实例之外,枚举类型没有其他实例。因此new
不能用于实例化枚举。
An enum type has no instances other than those defined by its enum constants. It is a compile-time error to attempt to explicitly instantiate an enum type (§15.9.1).
除了由其枚举常量定义的实例之外,枚举类型没有其他实例。尝试显式实例化枚举类型(第 15.9.1 节)是编译时错误。
Refer this
参考这个