FALL.ordinal()); // 2 System.out.println(SeasonEnum.WINTER.ordinal()); // 3 > public static <T extends Enum<T>> T valueOf(Class<T> enumType,String name):静态方法,用于返回指定枚举类中指定名称的枚举值。名称必须与在该枚举类中声明枚举值时所使用的标识符完全匹配,不允许使用额外的空白字符...
ConstantPool是一个抽象类,如果我们需要新建一个枚举类池,可以直接继承ConstantPool,然后实现其中的newConstant方法。下面是一个使用的具体例子: 代码语言:javascript 代码运行次数:0 运行 AI代码解释 public final class Foo extends AbstractConstant<Foo> { Foo(int id, String name) { super(id, name); } } ...
protected Enum(Stringname, int ordinal) Sole constructor. Programmers cannot invoke this constructor. It is for use by code emitted by the compiler in response to enum type declarations. Parameters: name- - The name of this enum constant, which is the identifier used to declare it. ...
枚举类型的每一个值都将映射到 protected Enum(String name, int ordinal) 构造函数中,在这里,每个值的名称都被转换成一个字符串,并且序数设置表示了此设置被创建的顺序。 上面那段代码声明的enum对象实际上调用了7次Enum(String name, int ordinal) 这个构造函数。(name 是常量,上面是MON,TUE等,ordinal是被创建...
System.out.println(Score.B.ordinal());//1,第2个枚举值 /*使用场景: * 实际开发中表单提交过来的值是字符串类型,需要验证此字符串是否是我们定义的枚举值 * 如果可以转换,说明提交的字符串没有问题,是我们预先定义好的枚举值 * 如果不可以转换,说明提交过来的字符串有误,会报非法参数异常,没有这个枚举值...
1.5.2、仅提供了两个属性 即我们上面用到的枚举常量名称(name)和枚举常量位置(ordinal) 贴上这两个属性的源码: /** * Returns the name of this enum constant, exactly as declared in its enum declaration. */ public final val name: String /** * Returns the ordinal of this enumeration constant...
enum Suit { Hearts = 0, Diamonds = 1, Clubs = 2, Spades = 3, } Enums can also have string values if specified explicitly. Values can be set based on some other value, even function definitions: enum FileAccess { // constant members None, Read = 1 << 1, Write = 1 << 2, Rea...
Each enum constant has aname()and anordinal()method. Thename()method returns a string representing the name of the constant (as defined), andordinal()returns the position of the constant (as defined). Once defined, the enum type and its constants are immutable. ...
clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf Methods inherited from class java.lang.Object getClass, notify, notifyAll, wait, wait, waitEnum Constant Detail DEFAULT public static final CacheHint DEFAULT No additional hint. The system will determin...
compareTo,equals,getDeclaringClass,hashCode,name,ordinal,valueOf Methods inherited from class java.lang.Object getClass,notify,notifyAll,wait,wait,wait Enum Constant Detail ElbRegistrationInProgress public static finalTargetHealthReasonEnumElbRegistrationInProgress ...