Illegal Forward Reference and Enums

前端 未结 11 1424
-上瘾入骨i
-上瘾入骨i 2020-12-08 09:46

I\'m programming a game in java which is made up of a grid of tiles. I wan\'t to be able to inuitively define the edges of the tiles and how they relate to each other, e.g.

11条回答
  •  一整个雨季
    2020-12-08 10:04

    With Java 8 lambdas:

    public enum Edge {
      TOP(() -> Edge.BOTTOM),
      BOTTOM(() -> Edge.TOP),
      LEFT(() -> Edge.RIGHT),
      RIGHT(() -> Edge.LEFT);
    
      private Supplier opposite;
    
      private Edge(Supplier opposite) {
        this.opposite = opposite;
      }
    
      public Edge opposite() {
        return opposite.get();
      }
    }
    

提交回复
热议问题