Getting Started with Java Lesson 59 - Method rewrite rules

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/houjunkang363/article/details/90598597

problem

        TetrominoT TetrominoJ overridden in class and the parent class Tetromino print method and test console output results are as follows:

    ---------打印T型---------
    i am a T
    (0,4),(0,5),(0,6),(1,5)
    - - - - * * * - - -
    - - - - - * - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
     ---------打印J型---------
     i am a J
     (0,4),(0,5),(0,6),(1,6)
     - - - - * * * - - -
     - - - - - - * - - -
     - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -

Program

        First, in TetrominoT class, override print method. The overriding method to follow the "two small with a large two" rules, like "with two" method name i.e., the same parameter list; "two small" refers to a subclass of method should return Return Type Value than the parent method smaller or equal to the subclass method declaration throws exception classes should be less than or equal to the parent class method declaration exception class thrown; "a large" refers to the method of access to a subclass should visit than the parent class method greater or equal rights. Secondly, print subclass TetrominoT rewritten, first of all to achieve the output "i am a T" in the console, and then, print method of the parent class is called using the super keyword.

        Then, in TetrominoJ class, override the print method in the class, still follow the above principles. Secondly, Print subclass TetrominoJ rewritable first implemented in the console output "i am a J", and then, Print call parent class using the super keyword.

        Finally, test TetrominoGame class, see the call method of the situation.

step

        This case needs to be achieved in the following steps.

Step 1: TetrominoT class, override print method

        First, the reconstruction TetrominoT class, override the print method in the class. Secondly, Print subclass TetrominoT rewritable first output achieve "i am a T" in the console, and then, Print call parent class using the super keyword code as follows:

    public class TetrominoT extends Tetromino{
        public TetrominoT(int row,int col){
            super();
            //按顺时针方向初始化Cell
            cells[0]=new Cell(row,col);
            cells[1]=new Cell(row,col+1);
            cells[2]=new Cell(row,col+2);
            cells[3]=new Cell(row+1,col+1);
        }
        
        @Override
        public void print(){
            System.out.println("i am a T");
            super.print();
        }
    }

Step two: TetrominoJ class, override print method

        First, the reconstruction TetrominoJ classes override print method; Secondly, print subclass TetrominoJ rewritable first implemented in the console output "i am a J", and then, calls the print method of the parent class using the super keyword. Code as follows:

    public class TetrominoJ extends Tetromino{
        public TetrominoJ(int row,int col){
            //按顺时针方向初始化Cell
            cells[0]=new Cell(row,col);
            cells[1]=new Cell(row,col+1);
            cells[2]=new Cell(row,col+2);
            cells[3]=new Cell(row+1,col+2);
        }
        
        @Override
        public void print(){
            System.out.println("i am a J");
            super.print();
        }
    }

Step three: Test

        Reconstruction TetrominoGame class, call the print method in its main method. Code as follows:

    public class TetrominoGame{
        public static void main(String[] args){
            //测试TetrominoT
            System.out.println("------打印T型-----");
            Tetromino t=new TetrominoT(0,4);
            
            t.print();
            
            printTetromino(t);
            
            //测试TetrominoJ
            System.out.println("------打印J型-------");
            Tetromino j=new TetrominoJ(0,4);
            
            j.print();
            
            printTetromino(j);
        }
        
        /**
         *打印出游戏所在的平面(宽10格,高20格)。用"-"号表示平面上的每个单元,用"*"
         号打印显示方块中的每个格子
         *
         * @param tetromino 需要显示在游戏平面中的方块
         */
         public static void printTetromino(Tetromino tetromino){
             int totalRow=20;
             int totalCol=10;
             //获取方块中存储的四个各自的数组
             Cell[] cells=tetromino.cells;
             for(int row=0;row<totalRow;row++){
                 for(int col=0;col<totalCol;col++){
                     //用于判断该位置是否包含在cells数组中
                     boolean isInCells=false;
                     for(int i=0;i<cells.length;i++){
                         if(cells[i].row==row&&cells[i].col==col){
                             System.out.print("* ");
                             isInCells=true;
                             break;
                         }
                     }
                     if(!isInCells){
                         System.out.print("- ");
                     }
                 }
                 System.out.println();
             }
         }
    }

        Console output results are as follows:

---------打印T型---------
    i am a T
    (0,4),(0,5),(0,6),(1,5)
    - - - - * * * - - -
    - - - - - * - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    - - - - - - - - - -
    ---------打印J型---------
        i am a J
        (0,4),(0,5),(0,6),(1,6)
        - - - - * * * - - -
        - - - - - - * - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -
        - - - - - - - - - -

        From the above output, when the object is a subclass directly assigned to the parent class reference variable, for example, the above code: when Tetromino t = new TetrominoT (0,4) ;, the t variable type is referenced compiled tetromino , and type of operation is TetrominoT. When run, if the child class overrides a parent class 8, then, when the reference variable method calls, method behavior has always shown its characteristic behavior of a subclass method, not the behavioral characteristics of the parent class method, That is, the call for subclasses. After the code above operation, the output of the "i am a T", "i am a J" also illustrates the print method called subclass.

Bo Main Comments:

    This lesson talked about "two with a big two small" rules are very important, pen questions often test. As long as you have mastered this principle, such a document questions you will not do anything wrong, you come to me wrong!

If this lesson helpful to you, please call reward me, your support is the greatest force me updated!

Scan code concern me:

Guess you like

Origin blog.csdn.net/houjunkang363/article/details/90598597