Java learning note 18 (Object class)

Keywords: Java

The Object class is the top-level parent class in Java. All classes are its children. The interface does not inherit it

Methods in the Object class:

Official data: boolean equals(Object obj) indicates whether another object is "equal" to this object

Example:

package demo;

public class Person extends Object {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Overriding the equals Method
    // Same source code,equals Is to compare the memory addresses of two objects

    public boolean equals(Object obj) {
        return this == obj;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

 

package demo;

public class TestEquals {
    public static void main(String[] args) {
        //Person Class inherited Object Class, inheriting the method of the parent class
        Person p1 = new Person("Zhang San", 18);
        Person p2 = new Person("Li Si", 20);
        boolean b1 = p1.equals(p2);
        System.out.println(b1);//output false
        p1 = p2;
        boolean b2 = p1.equals(p2);
        System.out.println(b2);//output true
        
    }
}

 

However, it is found that comparing memory addresses is meaningless. To meet some requirements, we can rewrite the equals method:

 

package demo;

public class Person extends Object {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Overriding the equals Method,Define your own way of comparison
    // Compare the age Same or not

    public boolean equals(Object obj) {
        // Dealing with the situation of the incoming
        if (this == obj) {
            return true;
        }
        // Handle incoming empty
        if (obj == null) {
            return false;
        }
        // Subclass features cannot be called in polymorphism, so downward transformation is required
        // join instanceof To prevent risks, because Object Is the top-level parent class, preventing other classes from being passed in
        if (obj instanceof Person) {
            Person p = (Person) obj;
            return this.age == p.age;
        } else {
            return false;
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
package demo;

public class TestEquals {
    public static void main(String[] args) {
        //Person Class inherited Object Class, inheriting the method of the parent class
        Person p1 = new Person("Zhang San", 20);
        Person p2 = new Person("Li Si", 20);
        boolean b1 = p1.equals(p2);
        System.out.println(b1);//output true
    }
}

 

toString method:

Official data: String toString() returns the string representation of the object

Continue with the Person class above to write an example:

package demo;

public class TestToString {
    public static void main(String[] args) {
        Person p1 = new Person("Zhang San", 18);
        String s1 = p1.toString();
        System.out.println(s1);
        System.out.println(p1);
    }
}
/*
The two outputs are the same:
demo.Person@2cdb03a1
demo.Person@2cdb03a1

So we found that if an object is written in the input statement, the toString method of the object is called by default
*/

 

In fact, there is no practical significance to get the memory address. In order to meet some requirements, we rewrite the toString method:

package demo;

public class Person extends Object {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    //Overriding the toString Method
    public String toString(){
        return this.name+this.age;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
package demo;

public class TestToString {
    public static void main(String[] args) {
        Person p1 = new Person("Zhang San", 18);
        String s1 = p1.toString();
        System.out.println(s1);
        System.out.println(p1);
    }
}
/*
The two outputs are always the same:
Zhang san18
 Zhang san18

We find that if an object is written in an input statement, the toString method of the object is always called by default
*/

 

There are other methods for Object, some of which are not commonly used. Some will be mentioned later

Posted by snicolas on Fri, 01 May 2020 09:30:36 -0700