如何修改它并利用多态性?

How can modify this and make use of polymorphism?

我正在做一个 Battleship 项目,一切正常。但我需要让它表现出多态性(使用多态性)。我有抽象的 class 细胞和子 classes 水。 怎样才能做到呢?这是我需要修改的代码部分:

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

任何时候你看到 instanceof 检查你应该看看你是否可以使用多态性。

而不是检查 board[pos1][pos2] 是否是水 ,例如,您可以简单地创建一个 board[pos1][pos2] 是什么类型的方法。

考虑这个方法header: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!");
}

然后,在您的主要方法中,您可以像这样在 board[pos1][pos2] 上简单地调用 doSomethingboard[pos1]][pos2].doSomething(pos1, pos2);

我会尝试类似下面的操作(但我的游戏和播放器可能 类 有误)。

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