Home > Java > javaTutorial > body text

How to define and use Java abstract classes and interfaces

WBOY
Release: 2023-05-05 19:43:05
forward
1472 people have browsed it

1. Comparison of objects

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

1.1 Comparable

is a parameter of the interface, which contains the type of the object to be compared.

There is only one compareTo abstract method in this interface, and the structure is as follows:

How to define and use Java abstract classes and interfaces

After implementing this interface in a class, you can compare the sizes between classes

1.2 Comparator

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:

How to define and use Java abstract classes and interfaces

How to define and use Java abstract classes and interfaces

and Comparable< ;T>The difference between the interfaces is that the Comparator interface can be used as a parameter of the sort method in the Arrays class

How to define and use Java abstract classes and interfaces

If it is an array whose elements are classes, use Comparator

2. Clone object

2.1 Cloneable

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:

How to define and use Java abstract classes and interfaces

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

2.2 Deep copy and Shallow copy

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 +
                &#39;}&#39;;
    }
}
Copy after login

We instantiate it in the main method and clone it to another object to see the result:

How to define and use Java abstract classes and interfaces

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 +
                &#39;}&#39;;
    }
}
Copy after login

The result is as follows:

How to define and use Java abstract classes and interfaces

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:

How to define and use Java abstract classes and interfaces

The modified memory of Class A is as follows :

How to define and use Java abstract classes and interfaces

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 +
                &#39;}&#39;;
    }
}
Copy after login

How to define and use Java abstract classes and interfaces

As for why you try drawing yourself, I won’t go into it here

3. Object class

There are some commonly used methods in the Object class here. Come out to introduce

3.1 equals

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); 
}
Copy after login

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

3.2 toString

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!