作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个像这样的类层次结构:
public abstract class GameController
public abstract class Game
我想使用一个通用的 GameController 类,因此它需要特定的 Game 子类,所以我将其更改为:
public abstract class GameController<GameType extends Game>
public abstract class Game
但后来我也想有一个通用的游戏来接受特定的玩家子类,所以我把它改成了:
public abstract class GameController<GameType extends Game<PlayerType>, PlayerType extends Player>
public abstract class Game<PlayerType extends Player>
有没有更好的方法来解决这个问题,这样我就不必两次声明 PlayerType(在 GameController 子类和 Game 子类中)?
编辑:
GameController 是一个网络类,它接收 post 和 get 请求,并将它们翻译成更接近游戏需要的东西,然后是一个子类,比如 MyGameController,进一步翻译被发布到方法调用的 byte[]具体的游戏类,比如 MyGame。代码示例:
游戏 Controller :
public abstract class GameController<GameType extends Game<PlayerType>, PlayerType extends Player> {
private Hashtable<String, GameType> games;
public MultiplayerMain() {
super();
games = new Hashtable<String, GameType>();
}
protected abstract GameType createGame(InputStream in, String gameId);
protected abstract PlayerType createPlayer(InputStream in, GameType game, String playerId);
protected abstract byte[] gameAction(InputStream in, GameType game, PlayerType player);
//Other stuff that calls createGame, createPlayer, and gameAction
}
游戏:
public abstract class Game<PlayerType extends Player> {
private Hashtable <String, PlayerType> players = new Hashtable<String, PlayerType>();
public final String gameId;
public Game (String id) {
gameId = id;
}
public abstract byte[] getGameState();
public abstract byte[] getGameState(PlayerType player);
final PlayerType getPlayer(String userId) {
return players.get(userId);
}
final void addPlayer(PlayerType player) {
players.put(player.userId,player);
playerAdded(player);
}
final void removePlayer(PlayerType player) {
players.remove(player);
playerRemoved(player);
}
protected void playerAdded(PlayerType player) {}
protected void playerRemoved(PlayerType player) {}
}
我的游戏 Controller :
public class MyGameController extends GameController<MyGame,MyPlaer> {
protected MyGame createGame(InputStream in, String gameId) {
byte[] data = getInitializationData(in);
return new MyGame(gameId,data);
}
protected byte[] gameAction(InputStream in, MyGame game, Player player) {
byte[] data = getData(in);
MyGame.methodSpecificToMyGameClassBasedOnInputStream(data);
return game.getGameState(player);
}
protected Player createPlayer(InputStream in, MyGame game, String playerId) {
byte[] otherData = getOtherData(in,game)
return new MyPlayer(playerId,otherData);
}
}
我的游戏:
public class MyGame extends Game<MyPlayer> {
public MyGame(String id) {
//...
}
public byte[] getGameState() {
//...
}
public byte[] getGameState(Player user) {
//...
}
public void methodSpecificToMyGameClassBasedOnInputStream(byte[] data) {
//...
}
}
最佳答案
你需要泛型吗?相反,您可以将 Player
注入(inject) Game
并将 Game
注入(inject) GameController
吗?
public class Game {
private final PlayerType playerType;
public Game(PlayerType playerType) {...}
}
public class GameController {
private final Game game;
public GameController(Game game) {...}
}
关于java - Java中泛型类的泛型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2298934/
我是一名优秀的程序员,十分优秀!