编程知识 cdmana.com

Introduction to Java lesson 91 -- overriding the equals method of the cell class

problem

    Java There are two ways to test whether two variables are equal in a program : One is to make use of == Operator , The other is to use equals.

     When using == When comparing two reference variables , They must point to the same object ,== Judgment will return true.

    equals The method is Object Class provides an instance method , Therefore, all reference variables can call this method to determine whether they are equal to other reference variables . But use this method to judge the equality of two objects == There is no difference between operators , It also requires two reference variables to point to the same object to return true. So the Object Class provides the equals The method doesn't have much practical significance , If you want to use a custom equivalent , You can use rewrite equals Method .

     This case requires that Cell Class equals Method , Two Cell The condition of reference variable equality is row row equality , Column column equality .

programme

     First , On the basis of the previous case , stay Cell Class equals Method , Two Cell The condition of reference variable equality is row row equality , Column column equality

     then , test equals Whether the method override works . First , In the class TestCell Add test method to testEquals, then , establish cell1 Objects and cell2 object , The code is as follows :

Cell cell1=new Cell(6,3);
Cell cell2=new Cell(6,3);

     Finally, we use == and equals Compare cell1 Objects and cell2 Whether objects are equal .

step

     To implement this case, follow the steps below .

Step one : stay Cell Class equals Method

     On the basis of the previous case , stay Cell Class equals Method , Two Cell Reference variable call equals Method returns ture The condition is that the rows are equal , Column column equality , The code is as follows :

    package day02;
    
    public class Cell{
        int row;
        int col;
        
        public Cell(int row,int col){
            this.row=row;
            this.col=col;
        }
        
        public Cell(){
            this(0,0);
        }
        
        public Cell(Cell cell){
            this(cell.row,cell.col);
        }
        
        public void drop(){
            row++;
        }
        
        public void moveRight(){
            col++;
        }
        
        public void moveLeft(){
            col--;
        }
        
        @Override
        public String toString(){
            return row+","+col;
        }
        
        @Override
        public boolean equals(Object obj){
            if(obj==null){
                return false;
            }
            if(this==obj){
                return true;
            }
            if(obj instanceof Cell){
                Cell cell=(Cell)obj;
                return cell.row==row&&cell.col==col;
            }else{
                return false;
            }
        }
    }

Step two : test equals Whether the method works

     test equals Whether the method override works . First , In the class TestCell Add test method to testEquals, then , establish cell1 Objects and cell2 object ; Finally, we use == and equals Compare cell1 Objects and cell2 Whether objects are equal , The code is as follows :

    package day02;
    
    import org.junit.Test;
    
    public class TestCell{
    
    /**
     * test equals Method 
     */
     @Test
     public void testEquals(){
         Cell cell1=new Cell(6,3);
         Cell cell2=new Cell(6,3);
         System.out.println(cell1==cell2);//false
         System.out.println(cell1.equals(cell2));//true
     }
    }

Step three : function

     function testEquals Method , The console output is as follows :

    false
    true

     As you can see from the results , Use == The result of the comparison is false、 Use equals The result of the comparison is true, explain Cell Class successfully covered equals Method , It realizes the equality of rows and columns Cell The reference variables of are equal .

Official account , Get a learning video

版权声明
本文为[Monkeys learn to program]所创,转载请带上原文链接,感谢

Scroll to Top