Java Object Oriented Programming Notes

1. Object class

In Java, only one class has no inheritance relationship, so this class is Object, that is, all classes are subclasses of Object by default.

class Person{}
class Person extends Object{}

The definitions of the above two classes are the same.
All inheritance problems are considered in the design of Object class, so this class provides parameter free construction methods, so that even if all classes do not know the existence of Object class, there will be no syntax error of construction method call failure.
Since the Object class is the parent of all classes, you can use the Object class to receive all subclass objects in this case.

class Person{}
public class Hello{
	public static void main(String args[]) {
		Object obj = new Person();
		if (obj instanceof Person) {
			Person per = (Person) obj;
			System.out.println("Person The object downward transformation is completed.");
		}
	}
}

If the method of a program requires that it can receive all class objects, it can use Object to realize processing.

2. Get object information: toString()

The Object class itself also provides some processing methods. In the Object class, you can obtain the complete information of an Object: public String toString().

class Person{}
public class Hello{
	public static void main(String args[]) {
		Person per = new Person();
		System.out.println(per);
		System.out.println(per.toString());//Inherited from Object class
		
	}
}

This method is the same as the method for direct output, so the acquisition of object information can directly override this method.

class Person{
	private String name;
	private int age;
	public Person(String name,int age) {
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "full name:"+this.name+",Age:"+this.age;
	}
}
public class Hello{
	public static void main(String args[]) {
		Person per = new Person("Zhang San",20);
		System.out.println(per);
	}
}

3. Object comparison: equals()

The main function of object comparison is to compare whether the contents of two objects are identical. If there are two Person objects, you want to confirm whether the two objects are consistent, but the two objects themselves will have different memory address values, so the comparison should be completed through content comparison.
Code: basic implementation of object comparison

class Person{
	private String name;
	private int age;
	public Person(String name,int age) {
		this.name = name;
		this.age = age;
	}
	public String toString() {
		return "full name:"+this.name+",Age:"+this.age;
	}
	public String getName() {
		return this.name;
	}
	public int getAge() {
		return this.age;
	}
}
public class Hello{
	public static void main(String args[]) {
		Person perA = new Person("Zhang San",20);
		Person perB = new Person("Zhang San",20);
		if (perA.getName().equals(perB.getName()) && 
				perA.getAge() == perB.getAge()) {
			System.out.println("Is the same object.");
		}else {
			System.out.println("Not the same object");
		}
	}
}
  • This comparison will judge the equality of each attribute, so a large number of getter() methods will be called.
  • Object comparison is an internal function of a class and should not be defined externally;

As the parent class of all classes, the Object class provides support for Object comparison operations. For the implementation of Object comparison operations, you can use equals():

  • Object comparison: public boolean equals(Object obj) can receive all types. By default, this method only judges the addresses of two objects;
public boolean equals(Object obj){
	return (this == obj);
}

To implement this method, you need to override this method in a subclass.

class Person{
	private String name;
	private int age;
	public Person(String name,int age) {
		this.name = name;
		this.age = age;
	}
	public String toString() {
		return "full name:"+this.name+",Age:"+this.age;
	}
	//The equals() method will have two objects at this time: the current Object this and the passed in Object
	public boolean equals(Object obj) {
		if(!(obj instanceof Person)) {
			return false;
		}
		if (obj == null) { //Don't care about null comparisons
			return false;
		}
		if (this == obj) { //Same address
			return true;
		}
		Person per = (Person) obj;//The purpose is to get the properties in the class
		return this.name.equals(per.name) && this.age == per.age;
	}
}
public class Hello{
	public static void main(String args[]) {
		Person perA = new Person("Zhang San",20);
		Person perB = new Person("Zhang San",20);
		System.out.println(perA.equals("dog"));
	}
}

Keywords: Java

Added by wata on Tue, 11 Jan 2022 05:08:49 +0200