我正在进行战舰项目,一切正常。但我需要让它表现多态(使用多态)。我有抽象类单元格和子类Water。 怎么能实现呢?这是我需要修改的代码部分:
public void fire(char position1, char position2){
int pos1 = (int) position1 - 64;
int pos2 = (int) position2 - 64;
if(pos1 > height || pos1 < 1 || pos2 > width || pos2 < 1){
System.err.println("Illegal coordinates.");
} else if(!(shooting_board[pos1][pos2] instanceof Water)){
System.err.println("Coordinates previously fired upon.");
} else if(board[pos1][pos2] instanceof Water){
shooting_board[pos1][pos2] = new MissedMissile();
totalMissilesFired++;
numberOfMisses++;
PlayBoard();
System.out.println("Miss!");
}else if(board[pos1][pos2] instanceof ShipSection){
String tmp = (board[pos1][pos2].toString());
shooting_board[pos1][pos2].damaged = true;
shooting_board[pos1][pos2] = new ShipSection("X");
//System.out.println(tmp);
int tmp2 = (int)(tmp).charAt(0);
//System.out.print(tmp2);
tmp2 = tmp2 - 65;
//System.out.println(tmp2);
Ship ship = ships.get(tmp2);
ship.hit();
if(ship.hasSunk == true){
sunk++;
System.out.println("Sunk!");
if(shipCount == sunk){
PlayBoard();
System.out.println("You win!");
printStats();
System.exit(0);
}
}
totalMissilesFired++;
numberOfHits++;
PlayBoard();
System.out.println("Hit!");
}
percentage = 100.0 * numberOfHits / totalMissilesFired;
if (totalMissilesFired == maxMissiles){
PlayBoard();
System.out.println("You lose!");
System.exit(0);
}
}
答案 0 :(得分:0)
每当您看到instanceof
检查时,您应该看看是否可以使用多态性。
例如,您可以简单地以board[pos1][pos2]
的任何类型创建方法,而不是检查board[pos1][pos2]
是否为水。
考虑此方法标题:void doSomething(int pos1, int pos2)
然后,您可以在子类中简单地实现(覆盖)它,例如:
@Override
public void doSomething(int pos1, int pos2) {
String tmp = (board[pos1][pos2].toString());
shooting_board[pos1][pos2].damaged = true;
shooting_board[pos1][pos2] = new ShipSection("X");
//System.out.println(tmp);
int tmp2 = (int)(tmp).charAt(0);
//System.out.print(tmp2);
tmp2 = tmp2 - 65;
//System.out.println(tmp2);
Ship ship = ships.get(tmp2);
ship.hit();
if(ship.hasSunk == true){
sunk++;
System.out.println("Sunk!");
if(shipCount == sunk){
PlayBoard();
System.out.println("You win!");
printStats();
System.exit(0);
}
}
totalMissilesFired++;
numberOfHits++;
PlayBoard();
System.out.println("Hit!");
}
然后,在您的主要方法中,您只需在doSomething
上致电board[pos1][pos2]
,就像这样:board[pos1]][pos2].doSomething(pos1, pos2);
答案 1 :(得分:0)
我会尝试下面的东西(但我可能让游戏和玩家类错了)。
class SO35948088 {
static class Game {
interface Fire {
void fire(Game game,Player player,int x,int y);
}
class Player {
void fire(int x,int y) {
shootingBoard[x][y].fire(Game.this,this,x,y);
}
int totalMissilesFired;
int numberOfMisses;
}
enum Type implements Fire {
water {
@Override public void fire(Game game,Player player,int x,int y) {
System.out.println("Miss!");
}
},
missedMissle {
@Override public void fire(Game game,Player player,int x,int y) {
// ??
}
},
shipSection {
@Override public void fire(Game game,Player player,int x,int y) {
System.out.println("Hit!");
}
}
}
Game(int n) {
this.n=n;
board=new Type[n][n];
shootingBoard=new Type[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
board[i][j]=shootingBoard[i][j]=Type.water;
}
final Player black=new Player(),white=new Player();
final int n;
final Type[][] board,shootingBoard;
}
public static void main(String[] args) {
Game game=new Game(10);
game.shootingBoard[0][1]=Game.Type.shipSection;
game.black.fire(0,0);
game.white.fire(0,1);
}
}