Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to define and use Java abstract classes and interfaces

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to define the use of Java abstract classes and interfaces". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to define the use of Java abstract classes and interfaces" can help you solve the problem.

1. Comparison of objects

Before comparing two objects, we first need to determine what to compare on. There are so many member variables in the object that it is impossible to compare directly.

1.1 Comparable

Is the parameter of the interface, which contains the type of object to be compared

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

After you implement this interface in a class, you can compare the size between the class and the class.

1.2 Comparator

There is an abstract method compare in this interface, which is also used to compare the size between objects. The structure of the method is as follows:

Unlike the Comparable interface, the Comparator interface can be used as a parameter to the sort method in the Arrays class

If it is an array with elements as classes, you can use the Comparator interface to further sort the array.

two。 Clone object 2.1 Cloneable

The clone () method that is automatically overridden in IDEA is as follows:

Throws and the following statements are not the focus now. Skip it for the time being.

Because the return value of this method is an Object class, remember to cast the result to a subclass when using it

2.2 Deep copy and shallow copy

Now that we're talking about cloning, we have to talk about deep copy and shallow copy, and briefly describe the concepts of both:

It should be explained that whether a clone method is a deep copy or a shallow copy is related to the member variables in the class and the code written by itself. Two different classes use the same cloning method, one is a deep copy and the other is a shallow copy. This situation exists.

There is now a class as follows:

Class An 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 does not change because of the change of b, and clone () is a deep copy.

Let's modify class An again:

Class B implements Cloneable {int k;} class An 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 results are as follows:

So is clone a deep copy or a shallow copy at this time?

The reason for this result is from a memory point of view, which is as follows before class An is modified:

The modified memory of class An is as follows:

According to the figure above, to make a deep copy, we need to clone the contents 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 An 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 to draw your own pictures, I won't talk about it here.

3.Object class

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

3.1 equals

In the Object class, this method is used to compare the size, and the return value is a Boolean value. The underlying implementation logic is as follows:

Public boolean equals (Object obj) {return (this = = obj);}

With regard to "=", if the variables on both sides are variables of the basic type, the comparison is whether the value is the same, and if it is a variable of the reference type, the comparison is whether the address is the same.

3.2 toString

The underlying call of the output statement System.out.println () is the toString method, but if the output is the reference type data, the default is the output modified address, so it needs to be rewritten at this time, which is why there is a toString method in the above example

This is the end of the introduction to "how to define the use of Java abstract classes and interfaces". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report