编程人 cdmana.com

Introduction to Java lesson 89 - object

1.1 Object

1.1.1 Object

    ·  stay Java Class inheritance structure ,java.lang.Object Class at the top ;

    ·  If you define a Java Class is not used extends Keyword declares its parent class , Then its parent class defaults to java.lang.Object class ;

    · Object A reference variable of type can point to any type of object .

1.2 toString Method

1.2.1 How to rewrite toString Method

    · Object Class , String representation used to return the value of an object ;

    ·  In principle, it is suggested to rewrite , Most formats follow " The name of the class [ Domain values ]", for example :

    public String toString(){
        return getClass().getName()
                +"[name="+name
                +",salary="+salary
                +",gender="+gender
                +"]";
    }

How to rewrite toString Method ( To continue 1)

    · Java Many places in the language call objects by default toString Method :

         character string + object , Automatically call the object's toString Method

        System.out.print( Any object ), Call directly toString Method

    ·  If you do not override toString Method , Will use Object Of toString Method , Its logic is :

         Class name @ Hash code

    · toString Method is a very useful debugging tool

    · JDK In the standard class library in , Many classes define toString Method , It is convenient for users to obtain necessary information about the object state ;

    ·  It is strongly recommended that you add... For each class you customize toString Method .

1.2.2 String rewrite toString Method

    · String Of toString() The way is to return to .

public String toString(){
        return this;
}

1.3 equals Method

1.3.1 equals Method

    · Object Medium equals Method , The function is to detect whether one object is equal to another ;

    ·  stay Object Class , This method determines whether two objects have the same reference , Is it the same object ;

    ·  in application , This method needs to be overridden , By comparing the member properties of an object , Make the method more meaningful , for example : about Cell class , If you do not override equals Method , The following code is in cell1 and cell2 Point to the same object true, It can be rewritten as : When x and y When coordinates are equal, two objects are equal , It makes more sense .

    cell1.equals(cell2);

1.3.2 How to rewrite equals Method

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

1.3.3 String rewrite equals Method

    · String Of equals Method is used to compare the character sequences of two string objects for equality .

    /**  Test string comparison  */
    public class Demo{
        public static void main(String[] args){
            String s1=new String("abc");
            String s2=new String("abc");
            String s3=new String("A");
            
            System.out.println(s1.equals(s2));//true
            System.out.println(s1.equals(s3));//false
        }
    }

1.3.4 equals and == The difference between

    · == Used to compare the values of variables , Can be applied to any type of , If used for reference type , Compare the values stored in two reference variables ( Address information ), Determine if two variables point to the same object ;

    · equals yes Object Methods , After rewriting , Can be used to compare the contents of two objects to see if “ equal ”;

    ·  It should be noted that ,Object default equals The comparison rule of the method is the same as ==

Official account , Get a learning video

Scroll to Top