Java Enum clone() 方法2024年11月8日 | 2 分钟阅读 Enum 类的 clone() 方法会抛出 CloneNotSupportedException。该方法确保枚举不能被克隆,这有助于维护其“单例”属性。 语法OverrideEnum 类的 clone() 方法重写了 Object 类的 clone() 方法。 返回值clone() 方法不返回任何内容。 抛出如果对象的类不支持 Cloneable 接口,则 clone() 方法会抛出 CloneNotSupportedException。重写此方法的子类也可以抛出 CloneNotSupportedException 异常,以表明实例不能被克隆。 示例 1输出 People with Red as their favourite colour are generally wiser. 示例 2输出 Maruti Car models till yet: MarutiI launched in 2010 year. MarutiII launched in 2013 year. MarutiIII launched in 2016 year. |
Enum 类的 `ordinal()` 方法返回此枚举常量的序号。此方法旨在供复杂的枚举数据结构使用,例如 `EnumSet` 和 `EnumMap`。语法 public final int ordinal() 返回值 `ordinal()` 方法返回此枚举常量的序号。示例 1 public class Enum_ordinalMethodExample1 { enum Colours{ Red,Green,Brown,Yellow; ...
阅读 2 分钟
Enum 类的 `getDeclaringClass()` 方法返回一个 Class 对象,该对象表示此枚举常量的枚举类型。假设有两个枚举常量 e1 和 e2,如果 `e1.getDeclaringClass() == e2.getDeclaringClass()`,则它们属于同一枚举类型。语法 public final Class<E>getDeclaringClass() 参数 NA 返回值 `getDeclaringClass()` 方法返回 Class...
阅读1分钟
`compareTo()` 方法的 Enum 类将此枚举对象与定义的 Other 对象进行顺序比较。枚举常量只能与同一类型的其他枚举常量进行比较。语法 public final int compareTo(E o) 参数 o - 这是要比较的枚举对象返回值 `compareTo()` 方法返回:负整数,...
阅读 2 分钟
Enum 类的 `equals()` 方法在与定义的 Other 对象相同时返回 true。语法 public final boolean equals(Object other) Override Enum 类的 `equals()` 方法会覆盖 Object 类的 `equals()` 方法。参数 other - 这是要与此枚举进行比较的对象。返回值...
阅读 2 分钟
Enum 类的 `finalize()` 方法不存在。语法 protected final void finalize() Override Enum 类的 `finalize()` 方法覆盖 Object 类的 `finalize()` 方法。示例 1 enum Vehicle{ MarutiI(2010), MarutiII(2013),MarutiIII(2016); int model; Vehicle(int m) { model = m; } int showModel() { return model; } } public class Enum_finalizeMethodExample1...
阅读1分钟
Enum 类的 `name()` 方法返回此枚举常量与枚举声明中声明的名称相同的名称。程序员通常使用 `toString()` 方法,因为它可能比 `name()` 方法返回一个更容易使用的名称。语法 public final String name() 返回值...
阅读 2 分钟
在 Java 中,枚举提供了一种方便的方式来定义一组命名常量。这些常量在枚举类型内声明,并可在整个代码中使用,从而提高可读性和类型安全性。枚举提供的关键方法之一是 valueOf(),它允许您...
阅读 3 分钟
Enum 类的 `toString()` 方法返回此枚举常量的名称,正如其声明中所包含的那样。可以覆盖 `toString()` 方法,但并非必需。语法 public String toString() 参数 NA Override:Enum 类的 `toString()` 方法覆盖 Object 类的 `toString()` 方法。返回值 `toString()` 方法返回此枚举...
阅读 2 分钟
Enum 类的 `hashCode()` 方法返回此枚举的哈希码。语法 public final int hashCode() 参数 NA Override Enum 类的 `hashCode()` 方法覆盖 Object 类的 `hashCode()` 方法。返回值 `hashCode()` 方法返回此枚举的哈希码。示例 1 public class Enum_hashCodeMethodExample1 { enum Seasons{ winter,summer,spring,rainy,autumn; } public...
阅读 2 分钟
我们请求您订阅我们的新闻通讯以获取最新更新。
我们提供所有技术(如 Java 教程、Android、Java 框架)的教程和面试问题
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India