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

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

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"属性:

enum Animal {
    DOG("woof"), CAT("meow");
    public String sound;

    Animal(String sound) {
        this.sound = sound;
    }

    @Override
    public String toString() {
        return sound;
    }
}

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

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

英文:

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

In Java 9+, if I do this:

enum Animal {
    DOG("woof"), CAT("meow");
    public String sound;

    Animal(String sound) {
      this.sound = sound;
    }
}

class Foo {
    public static void main(String[] args) {
        List<Animal> foo = List.of(Animal.DOG, Animal.CAT);
        System.out.println(Animal.values());
}

...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).

foo = {ImmutableCollections$List12@444} size =2
  0 = {Animal@448} "DOG"
     sound = "woof"
     name = "DOG"
     ordinal = 0
  1 = {Animal@449} "CAT"
     sound = "meow"
     name = "CAT"
     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":

import okhttp3.Protocol;

class Foo {
    public static void main(String[] args) {
        List<Protocol> foo = List.of(Protocol.HTTP_1_0, Protocol.HTTP_1_1);
        System.out.println(Animal.values());
}

...which you can see as follows:

foo = {ImmutableCollections$List12@562} size =2
  0 = {Protocol@566} http/1.0
    protocol = "http/1.0"
    name = "HTTP_1_0"
    ordinal = 0
  1 = {Protocol@567} http/1.1
    protocol = "http/1.1"
    name = "HTTP_1_1"
    ordinal = 1
}

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

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

答案1

得分: 2

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

override fun toString(): String = protocol

You can do this in Java enums as well:

enum Animal {
    DOG("woof"), CAT("meow");
    public String sound;

    Animal(String sound) {
      this sound = sound;
    }

    @Override
    public String toString() {
        return sound;
    }
}
英文:

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

override fun toString(): String = protocol

You can do this in Java enums as well:

enum Animal {
    DOG("woof"), CAT("meow");
    public String sound;

    Animal(String sound) {
      this.sound = sound;
    }

    @Override
    public String toString() {
        return sound;
    }
}

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:

确定