JAVA-Interface and Inheritance (7) Object Class

Object class is the parent class of all classes

When declaring a class, the default is to inherit Object

public class Hero extends Object

toString()

The Object class provides a toString method, so all classes have a toString method.
toString() means to return the string expression of the current object.
Printing an object through System.out.println is to print the toString() return value of the object.

package charactor;

public class test extends Object{
    
    
	String name;
	public String toString() {
    
    
		return name;
	}
	public static void main(String args[]) {
    
    
		test a  = new test();
		a.name = "德玛";
		System.out.println(a.toString());
		System.out.println(a);
	}
}

finalize()

When an object does not have any references to it, it satisfies the conditions for garbage collection

When it is garbage collected, its finalize() method will be called.

finalize() is not a method that developers actively call, but is called by the virtual machine JVM.

package charactor;

public class test extends Object{
    
    
	String name;
	public String toString() {
    
    
		return name;
	}
	public static void main(String args[]) {
    
    
		test b;
		for(int i = 0;i<100000;i++) {
    
    
			b  = new test();
		}
	}
	public void finalize() {
    
    
		System.out.println("这个对象正在被回收!!!");
	}
}

equals()

package charactor;

public class test extends Object{
    
    
	String name;
	int hp;
	public String toString() {
    
    
		return name;
	}
	public static void main(String args[]) {
    
    
		test a = new test();

		a.hp = 100;
		test b = new test();

		b.hp = 100;
		
		System.out.println(a.equals(b));
	}
	public boolean equals(Object o) {
    
    
		if (o instanceof hero) {
    
    
			hero h = (hero) o;
			return this.hp==h.hp;
		}
		return false;
	}
}

==

This is not an Object method, but it is used to determine whether two objects are the same.
More accurately, it is used to determine whether two references point to the same object.

	public static void main(String args[]) {
    
    
		test a = new test();
		a.hp = 100;
		test b = new test();
		b.hp = 100;
		test c = a;
		System.out.println(a==b);
		System.out.println(a==c);

	}

hashCode()

The hashCode method returns the hash value of an object, but it does not make sense to explain this method before understanding the meaning of the hash value.

The meaning of hashCode will be explained in the chapter on the principle of hashcode

Thread synchronization related methods

Object also provides thread synchronization related methods
wait()
notify()
notifyAll()
. The understanding of this part of the content needs to be based on a sufficient understanding of thread safety, so it will be explained in the chapter of thread interaction

getClass()

getClass() will return the class object of an object, which belongs to advanced content and will
not be explained here.

Exercise-Object⭐⭐Rewrite
the toString(), finalize() and equals() methods of
Item toString() Return the name of the Item + price
finalize() Output the current object is being recycled
equals(Object o) First determine whether o is an Item Type, and then compare whether the prices of the two items are the same

answer:

package charactor;

public class test extends Object{
    
    
	String name;
	int price;
	public String toString() {
    
    
		String tag = name + price;
		return tag;
	}
	public void finalize() {
    
    
		System.out.println("正在回收");
	}
	public boolean equals(Object o) {
    
    
		if(o instanceof test) {
    
    
			test h = (test) o;
			return this.price==h.price;
		}
	return false;
	}
	public static void main(String args[]) {
    
    
		test a = new test();
		a.name = "德玛西亚";
		a.price = 100;
		
		test b = new test();
		b.name = "锐雯";
		b.price = 100;
		
		System.out.println(a.equals(b));
	}
}

Guess you like

Origin blog.csdn.net/qq_17802895/article/details/108577333