[Alibaba cloud] Java object oriented development course notes (7) - Code Model 2

Comprehensive case: object comparison

To confirm whether two objects are equal, the complete information of the object should be compared, and the complete information of the object is the property of the object, so the object comparison refers to the comparison of the properties of two objects.
Example: Form 1 of object comparison

Code

class Person {
    private String name;
    private int age;
    public Person(String name,int age){
        this.name = name;
        this.age = 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;
    }
}
public class TestDemo {
    public static void main(String args[]) {
      Person perA = new Person("Zhang San",20);
      Person perB = new Person("Zhang San",20);
      // System.out.println(perA == perB); result: false
        // A complete comparison needs to be made according to the attribute information owned by the object
        if (perA.getName().equals(perB.getName()) && perA.getAge() == perB.getAge()){
            System.out.println("Two objects are equal!");
        }else{
            System.out.println("Two objects are not equal!");
        }
  }
} // Two objects are equal

Because the client (main method, call place) involves too much logic, this comparison form is basically not used. Object comparison should be a function of a class itself, not an external operation.
Example: implementation of object comparison II

  • Now suppose that the name of the method for object comparison is temporarily compare().

Code

class Person {
    private String name;
    private int age;
    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }
    // There will be two objects: the current object represented by this and the incoming object.
    public boolean compare(Person per){
        // At this time, the per object is already inside the class, so it is not restricted by encapsulation
        // It can be accessed directly by using the object. Property
        if (per == this){
            return true; // The incoming object has the same address as the current object
        }
       if (per == null){
          return false;
         }
        if (this.name.equals(per.name) && this.age == per.age){
            return true;
        }else {
            return false;
        }
    }
}
public class TestDemo {
    public static void main(String args[]) {
        Person perA = new Person("Zhang San",20);
        Person perB = new Person("Zhang San",20);
        if (perA.compare(perB)){
            System.out.println("Two objects are equal!");
        }else{
            System.out.println("Two objects are not equal!");
        }
    }
}// Two objects are equal

Object comparison is a function of a class itself. The core of comparison is to compare address, judge whether it is empty, and compare various attributes.

Tags: Attribute

Posted on Wed, 01 Apr 2020 15:42:26 -0700 by sks1024