Java接口字段到枚举列表

发布于 2025-01-20 03:06:06 字数 595 浏览 1 评论 0原文

如何从不同类中的交换机访问这些字段?我需要为每个 LocationType 做一个 switch case。

@FunctionalInterface
public interface LocationType {
    LocationType UNKNOWN = () -> "Unknown Location";
    LocationType MERGE = () -> "Merge Location";
    LocationType REGULAR = () -> "Regular Location";
    LocationType MULTI = () -> "Multi Location";
    LocationType ZONE = () -> "Zone Location";

    String getName();
}

例如,我稍后需要在另一堂课中做的是:

switch (locationList) {
    case LocationType.MERGE:
        break;
    case LocationType.MULTI:
        break;
}

how can I access these fields from a switch in a different class? I need to do a switch case for each LocationType.

@FunctionalInterface
public interface LocationType {
    LocationType UNKNOWN = () -> "Unknown Location";
    LocationType MERGE = () -> "Merge Location";
    LocationType REGULAR = () -> "Regular Location";
    LocationType MULTI = () -> "Multi Location";
    LocationType ZONE = () -> "Zone Location";

    String getName();
}

e.g. what I need to do later on, in another class is this:

switch (locationList) {
    case LocationType.MERGE:
        break;
    case LocationType.MULTI:
        break;
}

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

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

发布评论

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

评论(1

哑剧 2025-01-27 03:06:06

假设您只想通过字符串比较,而不是在此处的枚举列表中进行比较,则可能是一个可能的解决方案。

public enum Locations{   
   
     UNKNOWN{
          public String toString() {
            return "Unknown Location";
          } 
      },
     MERGE{
          public String toString() {
            return "Merge Location";
          } 
      },
    REGULAR{
          public String toString() {
            return "Regular Locaton";
          } 
      },
      MULTI{
          public String toString() {
            return "Multi Location";
          } 
      },
    ZONE{

          public String toString() {
            return "Zone Location";
          } 
      }, }

Assuming that you want to compare only by the String and not by the number of the enum list here it is a possible solution.

public enum Locations{   
   
     UNKNOWN{
          public String toString() {
            return "Unknown Location";
          } 
      },
     MERGE{
          public String toString() {
            return "Merge Location";
          } 
      },
    REGULAR{
          public String toString() {
            return "Regular Locaton";
          } 
      },
      MULTI{
          public String toString() {
            return "Multi Location";
          } 
      },
    ZONE{

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