枚举引用变量为什么不能单独在方法之外声明?

huangapple go评论101阅读模式
英文:

Why enum reference variables cannot be declared outside method on its own?

问题

  1. 我对于创建枚举类型的引用变量的规则感到困惑。
  2. **在主方法之外:**
  3. [A] 当我单独创建一个枚举引用变量时,会收到语法错误的提示。
  4. 然而,
  5. [B] 当我将这个引用变量与枚举常量连接在一起时,却没有问题。
  6. 当我尝试将 [A] [B] 放在主方法内部时,就像 [X] [Y]。
  7. **在主方法内部:**
  8. [B] [Y] 都没有问题。我能够创建一个与 [A] 类似的引用变量 [X],但却没有错误。
  9. 因此,我认为问题的根源在于 [A] 在主方法之外,而 [X] 在方法内部。
  10. 我可以在方法内部创建枚举的引用变量,但在方法外部这样做会产生语法错误。有人知道为什么会这样吗?非常感谢。
  11. enum Transport {
  12. CAR, TRUCK, AIRPLANE, TRAIN, BOAT
  13. }
  14. public class EnumDemo {
  15. Transport tp; // <A> - 在 ';' 处有语法错误。
  16. tp = Transport.AIRPLANE;
  17. Transport tb = Transport.AIRPLANE; // <B>
  18. public static void main(String[] args) {
  19. Transport tp; // <X> 但这是可以的。
  20. tp = Transport.AIRPLANE;
  21. Transport tb = Transport.AIRPLANE; // <Y>
  22. }
  23. }
英文:

I am confused by the rules when it comes to creating reference variables to enum types.

OUTSIDE MAIN METHOD:

[A] When I create a reference variable to enum on its own, I am greeted with a syntax error.
However,
[B] When I join this reference variable to an enum constant, it is somehow fine.

This situation is further complicated when I experiment with putting [A] and [B] inside the main method - as [X] and [Y].

INSIDE MAIN METHOD:

BOTH [X] and [Y] are fine. I am able to create a reference variable [X] exactly as in [A], but without error.

I therefore think the source of the problem is that [A] is outside main method, but [X] is within method.

I am able to create reference variable to enum inside a method, but doing so outside will produce syntax error. Does anyone know why this is so? Thank you very much.

  1. enum Transport {
  2. CAR, TRUCK, AIRPLANE, TRAIN, BOAT
  3. }
  4. public class EnumDemo {
  5. Transport tp; // <A> - syntax error on ';'
  6. tp = Transport.AIRPLANE;
  7. Transport tb = Transport.AIRPLANE; // <B>
  8. public static void main(String[] args) {
  9. Transport tp; // <X> but this is fine.
  10. tp = Transport.AIRPLANE;
  11. Transport tb = Transport.AIRPLANE; // <Y>
  12. }
  13. }

答案1

得分: 1

这与它是枚举无关:你不能在方法或代码块之外编写语句,只能声明成员。

以下方式有效:

  1. public class EnumDemo {
  2. Transport tp;
  3. {
  4. tp = Transport.AIRPLANE;
  5. }
  6. //...

{}tp = 周围被称为实例初始化程序;这些相对较少见,有些令人困惑,所以你应该更倾向于直接在字段上赋值(或者定义一个方法来封装必要的逻辑),或者在构造函数中赋值。

英文:

This is nothing specific to it being an enum: you just can't write statements outside methods or block, you can only declare members.

This would work:

  1. public class EnumDemo {
  2. Transport tp; // <A> - syntax error on ';'
  3. {
  4. tp = Transport.AIRPLANE;
  5. }
  6. //...

The {} around tp = is called an instance initializer; these are relatively uncommon, and somewhat confusing, so you should prefer assigning directly on the field (perhaps defining a method to encapsulate necessary logic), or in a constructor.

huangapple
  • 本文由 发表于 2020年5月4日 15:29:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/61587160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定