在 Java 枚举上实现 toString

发布于 2024-08-26 08:17:19 字数 481 浏览 6 评论 0原文

在Java中似乎可以写这样的东西:

 private enum TrafficLight {
  RED,
  GREEN;

  public String toString() {
   return //what should I return here if I want to return
                               //"abc" when red and "def" when green?
  }
 }

现在,我想知道当枚举的值为红色时是否可以在toString方法中返回“abc”,当枚举的值为绿色时返回“def”。另外,是否可以像在 C# 中一样,在哪里可以执行此操作?:

 private enum TrafficLight {
  RED = 0,
  GREEN = 15
  ...
 }

我已尝试过此操作,但出现编译器错误。

谢谢

It seems to be possible in Java to write something like this:

 private enum TrafficLight {
  RED,
  GREEN;

  public String toString() {
   return //what should I return here if I want to return
                               //"abc" when red and "def" when green?
  }
 }

Now, I'd like to know if it possible to returnin the toString method "abc" when the enum's value is red and "def" when it's green. Also, is it possible to do like in C#, where you can do this?:

 private enum TrafficLight {
  RED = 0,
  GREEN = 15
  ...
 }

I've tried this but it but I'm getting compiler errors with it.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

止于盛夏 2024-09-02 08:17:19

您可以按如下方式进行操作:

private enum TrafficLight {
   // using the constructor defined below
   RED("abc"),
   GREEN("def");

   // Member to hold the name
   private String string;

   // constructor to set the string
   TrafficLight(String name){string = name;}

   // the toString just returns the given name
   @Override
   public String toString() {
       return string;
   }
}

您可以添加任意数量的方法和成员。我相信你甚至可以添加多个构造函数。所有构造函数都必须是私有

Java 中的enum 基本上是一个具有一定数量实例的

You can do it as follows:

private enum TrafficLight {
   // using the constructor defined below
   RED("abc"),
   GREEN("def");

   // Member to hold the name
   private String string;

   // constructor to set the string
   TrafficLight(String name){string = name;}

   // the toString just returns the given name
   @Override
   public String toString() {
       return string;
   }
}

You can add as many methods and members as you like. I believe you can even add multiple constructors. All constructors must be private.

An enum in Java is basically a class that has a set number of instances.

话少心凉 2024-09-02 08:17:19

答案 1:

enum TrafficLight {
  RED,
  GREEN;

  @Override
  public String toString() {
    switch(this) {
      case RED: return "abc";
      case GREEN: return "def";
      default: throw new IllegalArgumentException();
    }
  }
}

答案 2:

enum TrafficLight {
  RED(0),
  GREEN(15);

  int value;
  TrafficLight(int value) { this.value = value; }
}

Ans 1:

enum TrafficLight {
  RED,
  GREEN;

  @Override
  public String toString() {
    switch(this) {
      case RED: return "abc";
      case GREEN: return "def";
      default: throw new IllegalArgumentException();
    }
  }
}

Ans 2:

enum TrafficLight {
  RED(0),
  GREEN(15);

  int value;
  TrafficLight(int value) { this.value = value; }
}
满地尘埃落定 2024-09-02 08:17:19

另外,如果您需要获取枚举的小写字符串值(“red”,“green”),您可以按如下方式执行:

private enum TrafficLight {
  RED,
  GREEN;

  @Override
  public String toString() {
   return super.toString().toLowerCase();
  }
}

Also if You need to get lowercase string value of enum ("red", "green") You can do it as follows:

private enum TrafficLight {
  RED,
  GREEN;

  @Override
  public String toString() {
   return super.toString().toLowerCase();
  }
}
爱已欠费 2024-09-02 08:17:19

我喜欢这种选择性替代 toString() 的方法,如果它对任何人都有用的话:

private enum TrafficLight {
  RED,
  GREEN {
    @Override
    public String toString() {
        return "GREEN-ISH";
    }
  }
}

I liked this approach for selective alternate toString() if it's useful for anyone out there :

private enum TrafficLight {
  RED,
  GREEN {
    @Override
    public String toString() {
        return "GREEN-ISH";
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文