本文介绍了如何使用java声明枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public enum myEnum {
ONE = 一,
TWO =two,
};
因为我想将这个常量类更改为枚举
public final class TestConstants {
public static String ONE =one;
public static String TWO =two;
}
解决方案
public enum MyEnum {
ONE(1),
TWO(2);
private int value;
private MyEnum(int value){
this.value = value;
}
public int getValue(){
返回值;
}
}
简而言之,您可以定义任意数量的参数枚举只要你提供构造函数的参数(并将值设置为相应的字段)
正如Scott所说 - 给出答案。始终从语言特征和结构的官方文档开始。
更新:对于字符串,唯一的区别是你的构造函数参数是 String
,并声明枚举与 TEST(test)
I want to convert this sample C# code into a java code:
public enum myEnum {
ONE = "one",
TWO = "two",
};
Because I want to change this constant class into enum
public final class TestConstants {
public static String ONE = "one";
public static String TWO= "two";
}
解决方案
public enum MyEnum {
ONE(1),
TWO(2);
private int value;
private MyEnum(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
In short - you can define any number of parameters for the enum as long as you provide constructor arguments (and set the values to the respective fields)
As Scott noted - the official enum documentation gives you the answer. Always start from the official documentation of language features and constructs.
Update: For strings the only difference is that your constructor argument is String
, and you declare enums with TEST("test")
这篇关于如何使用java声明枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!