Java : Enum 示例
Java 中的 Enum (Java SE 22 & JDK 22) 及其示例。
您将找到大多数 Enum<E extends Enum<E>> 方法的代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
enum Sample {
AAA,
BBB,
CCC,
}
System.out.println(Sample.AAA.name()); // AAA
System.out.println(Sample.AAA.ordinal()); // 0
System.out.println(Sample.BBB.name()); // BBB
System.out.println(Sample.BBB.ordinal()); // 1
System.out.println(Sample.CCC.name()); // CCC
System.out.println(Sample.CCC.ordinal()); // 2
Enum classes have implicitly declared methods below.
- public static T valueOf (String)
- public static T[] values ()
final Sample aaa = Sample.valueOf("AAA");
System.out.println(aaa); // AAA
final Sample[] values = Sample.values();
System.out.println(Arrays.toString(values)); // [AAA, BBB, CCC]
More information about enums, you can be found in section 8.9 of The Java Language Specification.
Constructors
Enum (String name, int ordinal)
Programmers can't invoke this constructor.
Methods
protected final Object clone ()
enum Sample {
AAA, BBB, CCC;
public void func() {
System.out.println("func! : " + name());
try {
clone();
} catch (CloneNotSupportedException e) {
System.out.println("CloneNotSupportedException!");
}
}
}
Sample.AAA.func();
// Result
// ↓
//func! : AAA
//CloneNotSupportedException!
final int compareTo (E o)
enum SampleX {
AAA,
BBB,
CCC,
}
enum SampleY {
CCC,
BBB,
AAA,
}
System.out.println(SampleX.AAA.compareTo(SampleX.AAA)); // 0
System.out.println(SampleX.AAA.compareTo(SampleX.BBB)); // -1
System.out.println(SampleX.AAA.compareTo(SampleX.CCC)); // -2
System.out.println(SampleY.AAA.compareTo(SampleY.AAA)); // 0
System.out.println(SampleY.AAA.compareTo(SampleY.BBB)); // 1
System.out.println(SampleY.AAA.compareTo(SampleY.CCC)); // 2
final Optional<Enum.EnumDesc<E>> describeConstable ()
enum Sample {
AAA,
BBB,
}
final var ret = Sample.AAA.describeConstable();
System.out.println(ret); // Optional[EnumDesc[Sample.AAA]]
final boolean equals (Object other)
enum SampleX {
AAA,
BBB,
}
enum SampleY {
AAA,
BBB,
}
System.out.println(SampleX.AAA.equals(SampleX.AAA)); // true
System.out.println(SampleX.AAA.equals(SampleX.BBB)); // false
System.out.println(SampleY.AAA.equals(SampleY.AAA)); // true
System.out.println(SampleY.AAA.equals(SampleY.BBB)); // false
System.out.println(SampleX.AAA.equals(SampleY.AAA)); // false
System.out.println(SampleY.BBB.equals(SampleX.BBB)); // false
protected final void finalize ()
Deprecated.
final Class<E> getDeclaringClass ()
enum Sample {
AAA,
BBB,
}
final var decA = Sample.AAA.getDeclaringClass();
System.out.println(decA); // class Sample
final var decB = Sample.BBB.getDeclaringClass();
System.out.println(decB); // class Sample
System.out.println(decA == decB); // true
final int hashCode ()
enum Sample {
AAA,
BBB,
CCC,
}
System.out.println(Sample.AAA.hashCode()); // 648786246
System.out.println(Sample.BBB.hashCode()); // 120360571
System.out.println(Sample.CCC.hashCode()); // 1710814638
final String name ()
The API documentation says "Most programmers should use the toString() method in preference to this one".
enum Sample {
AAA,
BBB,
CCC,
}
System.out.println(Sample.AAA.name()); // AAA
System.out.println(Sample.BBB.name()); // BBB
System.out.println(Sample.CCC.name()); // CCC
final int ordinal ()
enum Sample {
AAA,
BBB,
CCC,
}
System.out.println(Sample.AAA.ordinal()); // 0
System.out.println(Sample.BBB.ordinal()); // 1
System.out.println(Sample.CCC.ordinal()); // 2
String toString ()
enum Sample {
AAA,
BBB,
CCC,
}
final var str1 = Sample.AAA.toString();
System.out.println(str1); // AAA
final var str2 = Sample.BBB.toString();
System.out.println(str2); // BBB
final var str3 = Sample.CCC.toString();
System.out.println(str3); // CCC
static <T extends Enum<T>> T valueOf (Class<T> enumClass, String name)
enum Sample {
AAA,
BBB,
CCC,
}
final var aaa = Enum.valueOf(Sample.class, "AAA");
System.out.println(aaa); // AAA
final var bbb = Enum.valueOf(Sample.class, "BBB");
System.out.println(bbb); // BBB
try {
Enum.valueOf(Sample.class, "ZZZ");
} catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException! : " + e.getMessage());
}
// Result
// ↓
//IllegalArgumentException! : No enum constant null.ZZZ
Instead, it would be better to use the following method.
- public static T valueOf (String)
final var aaa = Sample.valueOf("AAA");
System.out.println(aaa); // AAA
相关文章
- API 示例