非法前向引用和枚举

| 我正在用Java编写游戏,该游戏是由一个瓷砖网格组成的。我将无法以独创的方式定义图块的边缘以及它们之间的相互关系,例如要获得图块的相对边缘,我希望能够只键入
TOP.opposite()
。但是,当使用枚举定义这些边时,我最终不得不在构造函数中转发至少其中两个引用:
public enum Edge {

   TOP(Edge.BOTTOM), //illegal forward reference
   BOTTOM(Edge.TOP),
   LEFT(Edge.RIGHT), //illegal forward reference
   RIGHT(Edge.LEFT);

   private Edge opposite;

   private Edge(Edge opp){
      this.opposite = opp;
   }

   public Edge opposite(){
      return this.opposite;
   }
}
有没有什么办法可以使用枚举来解决这个问题呢?     
已邀请:
您可以这样做不是那么直观。
public enum Edge {
    TOP, BOTTOM, LEFT, RIGHT;
    private Edge opposite;

    static {
        TOP.opposite = BOTTOM;
        BOTTOM.opposite = TOP;
        LEFT.opposite = RIGHT;
        RIGHT.opposite = LEFT;
    }
    public Edge opposite(){
        return this.opposite;
    }
}
    
enum Edge {
    TOP {
        @Override
        public Edge opposite() {
            return BOTTOM;
        }
    },
    BOTTOM {
        @Override
        public Edge opposite() {
            return TOP;
        }
    },
    LEFT {
        @Override
        public Edge opposite() {
            return RIGHT;
        }
    },
    RIGHT {
        @Override
        public Edge opposite() {
            return LEFT;
        }
    };

    public abstract Edge opposite();
}
    
public enum Edge {

    TOP,
    BOTTOM(Edge.TOP),
    LEFT,
    RIGHT(Edge.LEFT);

    private Edge opposite;

    private Edge() {

    }
    private Edge(Edge opp) {
        this.opposite = opp;
        opp.opposite = this;
    }

    public Edge opposite() {
        return this.opposite;
    }
}
    
这是另一种方式
public enum Edge {

    TOP(\"BOTTOM\"),
    BOTTOM(\"TOP\"),
    LEFT(\"RIGHT\"),
    RIGHT(\"LEFT\");

    private String opposite;

    private Edge(String opposite){
        this.opposite = opposite;
    }

    public Edge opposite(){
        return valueOf(opposite);
    }

}
但是,Peter Lawrey的解决方案更加高效且编译时安全。     
您还可以在枚举内部使用静态内部类:
public enum EnumTest     
{     
NORTH( Orientation.VERTICAL ),     
SOUTH( Orientation.VERTICAL ),     
EAST( Orientation.HORIZONTAL ),     
WEST( Orientation.HORIZONTAL );     

private static class Orientation  
{  
private static final String VERTICAL = null;     
private static final String HORIZONTAL = null;     
}
}
从这里被盗:)     
您可以定义一种与下面类似的方法。
public enum Edge {
    TOP,
    BOTTOM,
    LEFT,
    RIGHT;

    public Edge opposite() {
        switch (this) {
            case TOP:
                return Edge.BOTTOM;
            case BOTTOM:
                return Edge.TOP;
            case LEFT:
                return RIGHT;
            case RIGHT:
                return LEFT;
            default:
                throw new RuntimeException(\"Oh dear\");
        }
    }
}
    
您可以创建一个静态的“ 8”,其中key是原始枚举,而值是相反的边。在一个静态块中对其进行初始化,然后从
opposite()
方法返回映射。
private static Map<Edge, Edge> oppostiteMapping;

static {
  oppositeMapping = new EnumMap<Edge, Edge>();
  oppositeMapping.put(TOP, BOTTOM);
  ...
}

public Edge opposite() {
    return oppositeMapping.get(this);
} 
编辑:如在建议中更好地建议使用EnumMap,所以我相应地进行了升级 顺便说一句。当您创建静态
fromString()
方法等内容时,此方法通常很有用。     
您可以使用内部Map来定义这些关联。如果在初始化Map时已经创建了所有枚举值,则此方法有效:
public enum Edge {

  TOP,
  BOTTOM,
  LEFT,
  RIGHT;

  private static final Map<Edge, Edge> opposites = 
        new EnumMap<Edge, Edge>(Edge.class);
  static {
    opposites.put(TOP, BOTTOM);
    opposites.put(BOTTOM, TOP);
    opposites.put(LEFT, RIGHT);
    opposites.put(RIGHT, LEFT);
  }

  public Edge opposite(){
    return opposites.get(this);
  }
}
    
我的方法是使用序数。这是一个简单的示例,但更复杂的示例请参见下文。
public enum Edge {
    // Don\'t change the order! This class uses ordinal() in an arithmetic context.
    TOP,    // = 0
    LEFT,   // = 1
    RIGHT,  // = 2
    BOTTOM; // = 3

    public Edge other() {
        return values()[3 - ordinal()];
    }
}
尽管不鼓励使用序数形式,因为它是易碎的,但在与它所定义的枚举相同的枚举中使用序数形式则较不易碎,在此通过注释进一步加以缓解。尽管上面的示例非常琐碎,但下一个示例却并非如此。比较原始方式和使用序数方式: 从98行:
public enum Axes {
    NONE,
    HORIZONTAL,
    VERTICAL,
    BOTH;

    public Axes add(Axes axes) {
        switch (axes) {
            case HORIZONTAL:
                if (this == NONE)
                    return HORIZONTAL;
                if (this == VERTICAL)
                    return BOTH;
                break;
            case VERTICAL:
                if (this == NONE)
                    return VERTICAL;
                if (this == HORIZONTAL)
                    return BOTH;
                break;
            case BOTH:
                return BOTH;
            default:
                throw new AssertionError(axes);
        }
        return this;
    }

    public Axes remove(Axes axes) {
        switch (axes) {
            case HORIZONTAL:
                if (this == HORIZONTAL)
                    return NONE;
                if (this == BOTH)
                    return VERTICAL;
                break;
            case VERTICAL:
                if (this == VERTICAL)
                    return NONE;
                if (this == BOTH)
                    return HORIZONTAL;
                break;
            case BOTH:
                return NONE;
            default:
                throw new AssertionError(axes);
        }
        return this;
    }

    public Axes toggle(Axes axes) {
        switch (axes) {
            case NONE:
                return this;
            case HORIZONTAL:
                switch (this) {
                    case NONE:
                        return HORIZONTAL;
                    case HORIZONTAL:
                        return NONE;
                    case VERTICAL:
                        return BOTH;
                    case BOTH:
                        return VERTICAL;
                    default:
                        throw new AssertionError(axes);
                }
            case VERTICAL:
                switch (this) {
                    case NONE:
                        return VERTICAL;
                    case HORIZONTAL:
                        return BOTH;
                    case VERTICAL:
                        return NONE;
                    case BOTH:
                        return HORIZONTAL;
                    default:
                        throw new AssertionError(axes);
                }
            case BOTH:
                switch (this) {
                    case NONE:
                        return BOTH;
                    case HORIZONTAL:
                        return VERTICAL;
                    case VERTICAL:
                        return HORIZONTAL;
                    case BOTH:
                        return NONE;
                    default:
                        throw new AssertionError(axes);
                }
            default:
                throw new AssertionError(axes);
        }
    }
}
至19行:
public enum Axes {
    // Don\'t change the order! This class uses ordinal() as a 2-bit bitmask.
    NONE,       // = 0 = 0b00
    HORIZONTAL, // = 1 = 0b01
    VERTICAL,   // = 2 = 0b10
    BOTH;       // = 3 = 0b11

    public Axes add(Axes axes) {
        return values()[ordinal() | axes.ordinal()];
    }

    public Axes remove(Axes axes) {
        return values()[ordinal() & ~axes.ordinal()];
    }

    public Axes toggle(Axes axes) {
        return values()[ordinal() ^ axes.ordinal()];
    }
}
    
我更喜欢这样:
public enum Edge {
   TOP,
   BOTTOM,
   LEFT,
   RIGHT;

   private Link link;

   private Link getLink() {
     if (link == null) {
        link = Link.valueOf(name());
     }
     return link;
   }

   public Edge opposite() {
      return getLink().opposite();
   }
}

public enum Link {
   TOP(Edge.BOTTOM),
   BOTTOM(Edge.TOP),
   LEFT(Edge.RIGHT),
   RIGHT(Edge.LEFT);

   private Edge opposite;

   private Link(Edge opp) {
      this.opposite = opp;
   }

   public Edge opposite() {
      return this.opposite;
   }
}
    
使用Java 8 lambda:
public enum Edge {
  TOP(() -> Edge.BOTTOM),
  BOTTOM(() -> Edge.TOP),
  LEFT(() -> Edge.RIGHT),
  RIGHT(() -> Edge.LEFT);

  private Supplier<Edge> opposite;

  private Edge(Supplier<Edge> opposite) {
    this.opposite = opposite;
  }

  public Edge opposite() {
    return opposite.get();
  }
}
    

要回复问题请先登录注册