首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我们可以在ENUM中声明多少个变量

我们可以在ENUM中声明多少个变量
EN

Stack Overflow用户
提问于 2019-04-17 14:29:08
回答 2查看 140关注 0票数 0

我声明enum,如下所示:

代码语言:javascript
运行
复制
SUNDAY("Sunday","Week end","1",true)

在上面的enum中,我声明了四个变量,在枚举中有更多的变量是更好的方法吗?

EN

回答 2

Stack Overflow用户

发布于 2019-04-17 15:09:18

每个类或每个接口的常量池被ClassFile结构的16位constant_pool_count字段限制为65535条目。这是对单个类或接口的总复杂性的内部限制。

票数 2
EN

Stack Overflow用户

发布于 2019-04-17 15:32:21

正如评论中提到的,这主要取决于您的需求。

通常,在一个枚举上有多个属性是很好的,但是它们通常应该是恒定不变的( Kotlin中的val类型,Java语言中的final ),这取决于枚举在Java Virtual Machine中的共享方式。

如果您查看documentation from Oracle,您可以看到planets示例,其中定义了一些常量。

如果您最终拥有许多与枚举关联的常量,则可能需要重新考虑如何构建代码,并将相关概念封装到它们自己的对象中。

代码语言:javascript
运行
复制
public enum Planet {
  MERCURY(
          new PhysicalProperties(3.303e+23, 2.4397e6),
          new Information("Mercury", "foo")
  ),
  VENUS(
          new PhysicalProperties(4.869e+24, 6.0518e6),
          new Information("Venus", "foo")
  ),
  EARTH(
          new PhysicalProperties(5.976e+24, 6.37814e6),
          new Information("Earth", "Mostly Harmless")
  ),
  MARS(
          new PhysicalProperties(6.421e+23, 3.3972e6),
          new Information("Mars", "foo")
  ),
  JUPITER(
          new PhysicalProperties(1.9e+27, 7.1492e7),
          new Information("Jupiter", "foo")
  ),
  SATURN(
          new PhysicalProperties(5.688e+26, 6.0268e7),
          new Information("Saturn", "foo")
  ),
  URANUS(
          new PhysicalProperties(8.686e+25, 2.5559e7),
          new Information("Uranus", "foo")
  ),
  NEPTUNE(
          new PhysicalProperties(1.024e+26, 2.4746e7),
          new Information("Neptune", "foo")
  );



  private final PhysicalProperties physicalProperties;
  private final Information information;

  Planet(
          PhysicalProperties physicalProperties,
          Information information
  ) {
    this.physicalProperties = physicalProperties;
    this.information = information;
  }

  public PhysicalProperties physicalProperties() {
    return physicalProperties;
  }

  public Information information() {
    return information;
  }



  public static final class PhysicalProperties {
    private final double mass;   // in kilograms
    private final double radius; // in meters

    // universal gravitational constant  (m3 kg-1 s-2)
    static final double G = 6.67300E-11;

    PhysicalProperties(double mass, double radius) {
      this.mass = mass;
      this.radius = radius;
    }

    double surfaceGravity() {
      return G * mass / (radius * radius);
    }

    double surfaceWeight(double otherMass) {
      return otherMass * surfaceGravity();
    }

  }

  public static final class Information {

    private final String name;
    private final String description;

    Information(String name, String description) {
      this.name = name;
      this.description = description;
    }
  }

// I have omitted some getters from these examples for shortness
}

这是一个人为的例子,因为只有4个常量,但你可以想象有很多额外常量的情况。

当枚举膨胀时,您还应该考虑它们是否应该使用枚举类型,特别是当您发现自己在项目开发过程中添加了许多额外的枚举条目时。例如,要在上面的示例中添加一个新的planet,您需要添加一个新的枚举条目并重新编译,而您可以将该枚举转换为一个类,并动态创建任意数量的实例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55721217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档