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
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
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:
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: 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: 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 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: 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 method2. Clone object
2.1 Cloneable
2.2 Deep copy and Shallow copy
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 +
'}';
}
}
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 +
'}';
}
}
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 +
'}';
}
}
3. Object class
3.1 equals
public boolean equals(Object obj) {
return (this == obj);
}
3.2 toString
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!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools
