1, toString method in Object class:
java.lang.Object class is the root class in the Java language, that is, the parent class of all classes. All method subclasses described in it can be used. When an Object is instantiated, the final parent class is Object.
To create a Person class:
public class Person { private String name; private int age; public Person() { } 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; } }
The Person class inherits the object class by default, so you can use the toString method in the object class String toString() returns the string representation of the object.
public class Demo01ToString { public static void main(String[] args) { /* Person Class inherits the object class by default, so you can use the toString method in the object class String toString()Returns a string representation of the object. */ Person p=new Person("Zhang San",18); String s=p.toString(); System.out.println(s);//demo06.Person@2d98a335 , the address value of the object //Printing the name of the object directly is actually calling the toString method of the object, p=p.toString System.out.println(p);//demo06.Person@2d98a335 } }
It is meaningless to print the address value of the Object directly. You need to override the toString method of the Object class Print the properties of the object (name,age)
Add to the Person class:
public String toString(){ // return "abc"; return "Person{name="+name+" ,age="+age+"}"; }
perhaps
@Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; }
Check whether a class overrides the toString method, and print the name of the corresponding object of the class yourself If the toString method is not overridden, the address value of the object is printed (default) If the toString method is overridden, it is printed as overridden.
Random r=new Random(); System.out.println(r);//java.util.Random@4edde6e5 Scanner sc=new Scanner(System.in); System.out.println(sc);//java.util.Scanner[delimiters=\p{javaWhitespace} ArrayList<Integer> list=new ArrayList<>(); list.add(1); list.add(2); System.out.println(list);//[1, 2]
2, equals method in Object:
The Person class inherits the object class by default, so you can use the equals method in the object class boolean equals(Object obj) indicates whether some other object is "equal" to this object. Object class equals source code public boolean equals(Object obj) { return this == obj; } Parameters: Object: any object can be passed Method body: ==: the comparison operator returns Boolean values true and false Basic data type: value is compared Reference data type: compares the address values of two objects Who is this? The method called by that object, and this in the method is that object; p1 calls equals, and p1 is this Who's obj? Parameter p2 passed
public class Demo02equals { public static void main(String[] args) { Person p1=new Person("Yang Mi",24); Person p2=new Person("Gao Yuanyuan",22); boolean b=p1.equals(p2);//Compare address values System.out.println(b);//false p1=p2;//Assign the address value of p2 to p1 boolean c=p1.equals(p2); System.out.println(c);//true } }
The equals method of object class compares the address values of two objects by default, which is meaningless So we need to override the equals method to compare the attribute values (name,age) of the two objects The property value of the object is the same. It returns true. Otherwise, it returns false Question: Implies a polymorphism Object obj=p2=new Person("high circle", 22); Polymorphism disadvantage: you cannot use subclass specific content (properties, methods) Solution: you can use downward transformation (forced) to transform object into Person type
Add the following to the Person class:
@Override public boolean equals(Object obj) { //Add a judgment. The transfer parameter obj is this itself, which directly returns true to improve program efficiency if (obj==this){ return true; } //Add a judgment that the passed parameter obj is null and returns false directly to improve program efficiency if (obj==null){ return false; } /* Add a judgment that the Person type is converting to prevent the type conversion exception ClassCastException */ if (obj instanceof Person) { // Use the downward transformation (force) to convert the object type to Person Person p = (Person) obj; //Compare the attributes of two objects. One is this (p1) of the calling method, and the other is p (obj=p1) boolean b = this.name.equals(p.name) && this.age == p.age; return b; }else { return false; } }
Just run it again.