Before comparing two objects, we first need to determine what to compare based on. There are so many member variables in the object, direct comparison is impossible
There is only one compareTo abstract method in this interface, and the structure is as follows:
After implementing this interface in a class, you can compare the sizes between classes
This There is an abstract method compare in the interface, which is also used to compare objects. The structure of the method is as follows:
and Comparable< ;T>The difference between the interfaces is that the Comparator
If it is an array whose elements are classes, use Comparator
This interface is an empty interface, but the class needs to implement this interface to be cloned. The overridden method is the clone() method in the Object class
The clone() method automatically rewritten in IDEA is as follows:
throws and The following statements are not the focus now and will be skipped for the time being.
Because the return value of this method is the Object class, remember to force type conversion of the result into a subclass when using it
Since we are talking about cloning, we have to mention deep copy and shallow copy. Let’s briefly talk about the concepts of the two:
Assume that the content of A is copied to B, and then we Modify the content in B. If the content of A has not changed, then it is a deep copy, otherwise it is a shallow copy.
What needs to be explained is: whether a cloning method is a deep copy or a shallow copy and the member variables in the class and itself The code you write is related. Two different classes use the same cloning method. One is a deep copy and the other is a shallow copy. This situation exists.
Now there is a class as follows:
class A implements Cloneable{ int i; int j; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } @Override public String toString() { return "{" + "i=" + i + ", j=" + j + '}'; } }
We instantiate it in the main method and clone it to another object to see the result:
At this time, a has not changed due to the change of b. , clone() is a deep copy
We will transform class A:
class B implements Cloneable { int k; } class A implements Cloneable{ int i; int j; B c=new B(); @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } @Override public String toString() { return "{" + "i=" + i + ", j=" + j + ", c.k=" + c.k + '}'; } }
The result is as follows:
Then clone at this time Is it a deep copy or a shallow copy?
The reason for this result depends on the memory. Before Class A is modified, the memory is as follows:
The modified memory of Class A is as follows :
According to the above figure, to achieve deep copy, we need to clone the content of class B again, so we need to modify the clone method
class B implements Cloneable { int k; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } } class A implements Cloneable{ int i; int j; B c=new B(); @Override protected Object clone() throws CloneNotSupportedException { A tmp=(A)super.clone(); tmp.c=(B)this.c.clone(); return tmp; } @Override public String toString() { return "{" + "i=" + i + ", j=" + j + ", c.k=" + c.k + '}'; } }
As for why you try drawing yourself, I won’t go into it here
There are some commonly used methods in the Object class here. Come out to introduce
In the Object class, this method is used to compare sizes. The return value is a Boolean value. The underlying implementation logic is as follows:
public boolean equals(Object obj) { return (this == obj); }
About " ==", if the variables on both sides are basic type variables, the comparison is whether the values are the same, and if they are reference type variables, the comparison is whether the addresses are the same
The bottom layer of the output statement System.out.println() calls the toString method. However, if the output is reference type data, the modified address is output by default, so it needs to be rewritten at this time. This is also the case in the above example. Reasons for toString method
The above is the detailed content of How to define and use Java abstract classes and interfaces. For more information, please follow other related articles on the PHP Chinese website!