Illegal Forward Reference and Enums

前端 未结 11 1429
-上瘾入骨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 09:53

    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;
        }
    }
    

提交回复
热议问题