gpt4 book ai didi

java - Android:枚举与静态最终整数?

转载 作者:IT老高 更新时间:2023-10-28 21:03:04 28 4
gpt4 key购买 nike

在 Java Android 应用程序中使用 enum 与使用一组 static final int 有哪些优点(或缺点)?是否存在有利于使用其中一种的效率或性能优化?

我在说 Intent requestCodes 等的上下文中问这个 - 在 Android 示例代码中往往是整数,而不是我在 C 中习惯的枚举值。

最佳答案

this question 的枚举优势:

  • They are much more type-safe than integers, strings, or sets of boolean flags.
  • They lead to more readable code.
  • It's more difficult to set an enum to an invalid value than an int or string.
  • They make it easy to discover the allowed values for a variable or parameter.
  • Everything I've read indicates that they perform just as well as integers in C# and most JVMs.

我会补充:

  • 枚举可以有成员变量和实例变量,而 int 不能。

像大多数抽象一样,一旦 performance catches up .特别是在您的应用程序代码中(相对于框架代码),我会选择枚举而不是其他模拟它们的方法。

关于java - Android:枚举与静态最终整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5252465/

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