cyclical generics (try 2)

前端 未结 2 1621
甜味超标
甜味超标 2020-12-11 08:50

Second attempt at this question (the initial code wasn\'t enough to highlight the issue)

Here is the code that does not compile:

interface Player<         


        
2条回答
  •  一整个雨季
    2020-12-11 09:24

    As Paul Bellora points out, you're mixing generic and raw types -- and the correct, fully-generic solution is a bit of a mess and requires a lot of redundancy. There's no nice way (that I know of) to do circular (but not recursive) generics in Java.

    Rather than struggling with this, I would make both Player and Game generic on just one parameter, the type of value being played with -- what you had as R.

    interface Game {
        void play(Player player);
    }
    
    interface Player {
        R takeTurn(Game game);
    }
    
    abstract class AbstractGame implements Game {
        public final void play(Player player) {
            final R value;
    
            value = player.takeTurn(this);
            turnTaken(value);
        }
    
        protected abstract void turnTaken(R value);
    }
    
    class XPlayer implements Player {
        @Override
        public Integer takeTurn(Game game) {
            return 42;
        }
    }
    
    class XGame extends AbstractGame {
        @Override
        public void turnTaken(Integer value) {
            System.out.println("value = " + value);
        }
    }
    
    public class Main {
        public static void main(String[] argv) {
            XPlayer player = new XPlayer();
            XGame game = new XGame();
            game.play(player);
        }
    }
    

    Now, any player who knows how to take R-based moves can play any R-based game.

提交回复
热议问题