简体   繁体   English

为什么Java中“枚举”的实例字段是“枚举”?

[英]Why instance field of 'enum' is 'enum' in java?

For the below TypeAndSize class, 对于下面的TypeAndSize类,

class TypeAndSize {

  Species type;               // runType EMPTY, SHARK, or FISH
  int size;                   // Number of cells in the run for that runType.

  enum Species{EMPTY,SHARK,FISH}

  /**
   *  Constructor for a TypeAndSize of specified species and run length.
   *  @param species is Ocean.EMPTY, Ocean.SHARK, or Ocean.FISH.
   *  @param runLength is the number of identical cells in this run.
   *  @return the newly constructed Critter.
   */

  TypeAndSize(Species species, int runLength) {
    if (species == null)    {   
      System.out.println("TypeAndSize Error:  Illegal species.");
      System.exit(1);
    }
    if (runLength < 1) {
      System.out.println("TypeAndSize Error:  runLength must be at least 1.");
      System.exit(1);
    }
    this.type = species;
    this.size = runLength;
  }

}

with the usage of member fields of enum class type in below code, 在下面的代码中使用enum类类型的成员字段,

class RunLengthEncoding {
    ...
    public RunLengthEncoding(int i, int j, int starveTime) {
          this.list = new DList2();
          this.list.insertFront(TypeAndSize.Species.EMPTY, i*j);
          ....
      }
    ...
}

let me ask this question. 让我问这个问题。

My question: Why member field of enum class designed to be an instance of enum ? 我的问题:为什么将enum类的成员字段设计为enum的实例? Because of the ease in passing on the parameters of enum class type which can be backward compatible with old concept of enum in C language which was a collection of constants? 因为易于传递enum类类型的参数,而该参数可以向后兼容C语言中旧的enum概念,而C语言是常量的集合? Is that the reason? 那是原因吗?

What you are talking about ( TypeAndSize.Species.EMPTY ) is not a member field of Spicies . 你所谈论( TypeAndSize.Species.EMPTY )不是的成员场Spicies When we talk about "member field", it normally implies instance variable (for which it is also possible to write in enum in Java). 当我们谈论“成员字段”时,它通常暗含实例变量(也可以用Java enum编写实例变量)。

In the aspect you are asking, you can simply interpret enum being a special shorthand of writing a special class with class constants: 在您要求的方面,您可以简单地将enum解释为编写带有类常量的特殊类的特殊速记:

enum Foo {
  A,
  B;
}

is similar to 类似于

class Foo {
    public static final Foo A = new Foo();
    public static final Foo B = new Foo();

    private Foo() {}
}

(There are still a lot of difference between enum and the handcrafted class, but they are not yet your concern) (枚举和手工制作的类之间仍然有很多区别,但是它们并不是您所关心的)

with the usage of member fields of enum class type 使用枚举类类型的成员字段

TypeAndSize.Species.EMPTY is not using a member field. TypeAndSize.Species.EMPTY没有使用成员字段。 It is access the enum type defined in TypeAndSize. 它可以访问TypeAndSize中定义的枚举类型。

Species type is defining a member field but it's type isn't enum, it's type is Species. Species type定义了一个成员字段,但是它的类型不是枚举,而是“种类”。

Enum is on the same level as class and interface. 枚举与类和接口处于同一级别。 These are the building block types we can use. 这些是我们可以使用的构件类型。 The reason that we want to use enums over integer constants can be read elsewhere . 我们想在整数常量上使用枚举的原因可以在其他地方阅读。

Hopefully the link link helps answer the second part of your question. 希望链接链接可以帮助回答问题的第二部分。
I've tried my best to clear up the terms used in the question. 我已尽力清除问题中使用的术语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM