Illegal Forward Reference and Enums

前端 未结 11 1425
-上瘾入骨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:12

    You could use an internal Map instead to define these associations. This works if at the point of initializing the Map, you already have all enum values created:

    public enum Edge {
    
      TOP,
      BOTTOM,
      LEFT,
      RIGHT;
    
      private static final Map opposites = 
            new EnumMap(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);
      }
    }
    

提交回复
热议问题