为什么Java枚举中将自身称为其 “name” 而不是像Kotlin中那样称其为 “value”?

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

Why does a Java enum refer to itself as its "name" not its "value" like in Kotlin?

问题

在Java中,枚举类型的默认行为是使用枚举元素的"name"属性,而在Kotlin中,枚举元素默认使用枚举元素的"value"属性。如果你希望在Java中的枚举中也像Kotlin一样使用"value"属性,你可以通过重写toString()方法来实现。

下面是一个示例,在Java中如何使枚举元素使用"value"属性:

  1. enum Animal {
  2. DOG("woof"), CAT("meow");
  3. public String sound;
  4. Animal(String sound) {
  5. this.sound = sound;
  6. }
  7. @Override
  8. public String toString() {
  9. return sound;
  10. }
  11. }

通过重写toString()方法,你可以确保在调试器或其他情况下,枚举元素显示的值是它们的"value"属性,而不是默认的"name"属性。

请注意,这只会影响枚举元素的显示方式,不会更改它们的实际行为或属性。

英文:

Disclaimer: I'm new to Java and Kotlin enums.

In Java 9+, if I do this:

  1. enum Animal {
  2. DOG("woof"), CAT("meow");
  3. public String sound;
  4. Animal(String sound) {
  5. this.sound = sound;
  6. }
  7. }
  8. class Foo {
  9. public static void main(String[] args) {
  10. List<Animal> foo = List.of(Animal.DOG, Animal.CAT);
  11. System.out.println(Animal.values());
  12. }

...and then set a breakpoint on the println(), my IntelliJ IDEA debugger shows that each enum element calls itself (e.g., {Animal@448}) by its "name" not its "value" (sound).

  1. foo = {ImmutableCollections$List12@444} size =2
  2. 0 = {Animal@448} "DOG"
  3. sound = "woof"
  4. name = "DOG"
  5. ordinal = 0
  6. 1 = {Animal@449} "CAT"
  7. sound = "meow"
  8. name = "CAT"
  9. ordinal = 1

But when I try similar code using okhttp.Protocol (an enum written in Kotlin), I see the opposite happen: each element refers to itself (e.g., Protocol@566) using the "value" (protocol) instead of the "name":

  1. import okhttp3.Protocol;
  2. class Foo {
  3. public static void main(String[] args) {
  4. List<Protocol> foo = List.of(Protocol.HTTP_1_0, Protocol.HTTP_1_1);
  5. System.out.println(Animal.values());
  6. }

...which you can see as follows:

  1. foo = {ImmutableCollections$List12@562} size =2
  2. 0 = {Protocol@566} http/1.0
  3. protocol = "http/1.0"
  4. name = "HTTP_1_0"
  5. ordinal = 0
  6. 1 = {Protocol@567} http/1.1
  7. protocol = "http/1.1"
  8. name = "HTTP_1_1"
  9. ordinal = 1
  10. }

How can I make Java use the "value" like in Kotlin? For reference, here's the relevant Kotlin "primary constructor":

  1. enum class Protocol(private val protocol: String) {
  2. /**
  3. * An obsolete plaintext framing that does not use persistent sockets by default.
  4. */
  5. HTTP_1_0("http/1.0"),

答案1

得分: 2

In the source code of Protocol, you can see that they overrode toString():

  1. override fun toString(): String = protocol

You can do this in Java enums as well:

  1. enum Animal {
  2. DOG("woof"), CAT("meow");
  3. public String sound;
  4. Animal(String sound) {
  5. this sound = sound;
  6. }
  7. @Override
  8. public String toString() {
  9. return sound;
  10. }
  11. }
英文:

In the source code of Protocol, you can see that they overrode toString():

  1. override fun toString(): String = protocol

You can do this in Java enums as well:

  1. enum Animal {
  2. DOG("woof"), CAT("meow");
  3. public String sound;
  4. Animal(String sound) {
  5. this.sound = sound;
  6. }
  7. @Override
  8. public String toString() {
  9. return sound;
  10. }
  11. }

huangapple
  • 本文由 发表于 2023年3月10日 00:52:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687669.html
匿名

发表评论

匿名网友

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

确定