Java 成员枚举类型应该大写吗?

发布于 2024-12-15 18:09:29 字数 1431 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你丑哭了我 2024-12-22 18:09:29

枚举是一种类型,枚举名称应以大写字母开头。枚举成员是常量,其文本应全部大写。

Enums are a type and the enum name should start with a capital. Enum members are constants and their text should be all-uppercase.

少女情怀诗 2024-12-22 18:09:29

如果他们是自己的班级,则以大写开头;如果他们是成员,则以小写开头。

public enum ReportType { XML, TEXT, HTML };

public class MyClass
{
     ReportType defaultReport = ReportType.XML; 
}

If they are their own class start with upper case, if they are members lower case.

public enum ReportType { XML, TEXT, HTML };

public class MyClass
{
     ReportType defaultReport = ReportType.XML; 
}
温折酒 2024-12-22 18:09:29

您确定使用的是默认设置吗?因为一般来说枚举确实是大写的。不过,保存枚举值的变量不应该以上限开头。

public enum State {
  UNINITIALIZED,
  INITIALIZED,
  STARTED,
  ;
}

private State state;

private void start() {
  state = State.UNINITIALIZED;
  ...
}
`.

您也可以使用静态导入来删除 State. 部分,尽管通常我认为最好保留它。枚举值是常量并且应该大写。我见过人们在运行时更改枚举常量中的字段,但这不是您想要的(除了类本身内部时不时的延迟实例化)。

Are you sure you are using the default settings? Because generally enums are indeed capitalized. Variables holding enum values should not start with a cap though.

public enum State {
  UNINITIALIZED,
  INITIALIZED,
  STARTED,
  ;
}

private State state;

private void start() {
  state = State.UNINITIALIZED;
  ...
}
`.

You may use static imports to get rid of the State. part as well, although generally I think it is better to leave it be. The enum values are constants and should be in CAPS. I've seen people change fields in enum constants during runtime, and that is not what you want (except for lazy instantiation within the class itself now and then).

落在眉间の轻吻 2024-12-22 18:09:29

甚至翻阅了Java约定文档,但没有看到引用这个问题。

Java 约定文档已经 20 多年没有更新了。基本上从 Java 1.1 开始。

Even flipped through the Java conventions doc, but didn't see this issue referenced.

The Java conventions doc hasn't been updated in 20+ years. Basically since Java 1.1.

西瓜 2024-12-22 18:09:29

在 Java 中,(非原始)类型的名称通常以 PascalCase 形式编写。由于enum(作为class)定义了一种类型,因此通常也用PascalCase来编写它们:

enum MyEnumInPascalCase { ... }

In Java, names of (non-primitive) types are usually written in PascalCase. Since an enum (as a class) defines a type, it is usual to write them also in PascalCase:

enum MyEnumInPascalCase { ... }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文