gpt4 book ai didi

具有多个参数的 Java 枚举

转载 作者:行者123 更新时间:2023-11-29 07:25:58 24 4
gpt4 key购买 nike

我想知道在枚举类中存储多个值的方法是什么?

我试过了

public enum name { 
James(1,2,3)
Taylor(2,3,4)
Mary(5,6,7);
}

但这会给我一个错误。

最佳答案

关于这一点,枚举就像普通类一样:您将需要一个构造函数。

private Name(int a, int b, int c) {
// Define some fields and store a b and c into them
}

请注意,在类主体中,必须首先定义枚举常量,然后是可选字段、构造函数和方法:

enum Name {
ENUM_CONSTANT_1, ENUM_CONSTANT_2;

int field;

Name() { ... }

public void someMethod() { ... }

}

注意:您应该遵循 Java 命名约定:类名(包括枚举名称)始终以大写字母开头。

关于具有多个参数的 Java 枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52730594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com