当前位置:网站首页>Object class not ended
Object class not ended
2022-06-12 05:00:00 【なんでもないゃ】
Object Class is Java Base class of all classes , Is the top of the entire class inheritance structure , It's also the most abstract class .
Object contains :registerNatives()、getClass()、hasCode()、equals()、clone()、toString()、notify()、notifyAll()、wait(long)、wait(long,int)、wait()、finalize() Twelve methods .
registerNatives()
getClass()
public final natove Class getClass();
public Methods can be called directly through objects ,getClass The method is to get the collection of all the information of the object of the current class at runtime .
hashCode()
public native int hashCode();
public Method subclasses can override it ,hashCode Method returns the address of the current object by default hashCode value , After rewriting, it is the custom calculation method , The value is in the range of integers (-2^31~2^31-1).
import java.util.stream.Stream;
public class lengthannotation {
private int age;
private String name;
// Pay attention to the creation and use of constructors ;
lengthannotation(int age,String name){
this.age=age;
this.name=name;
}
public int hashCode () {
//Stream.of() Create a sequential flow for the specified element ;
//Stream.toArray() Method returns Object[] Array ;
Object[] a = Stream.of(age, name).toArray();
int result = 1;
for (Object element : a) {
result = 31 * result + (element == null ? 0 : element.hashCode());
}
return result;
}
public static void main(String[] args) {
// Call... After creating the object hashCode();
lengthannotation length=new lengthannotation(1," Zhao Zhao ");
System.out.println(length.hashCode());
}
}equals()
public boolean equals(Object obj);
Used to compare whether the current object and the target object are equal , The default is to compare references ( Object address ) Whether to point to the same object , Subclasses can override , After being overridden within a class, the overridden equals Method .
The reason for rewriting : When defining an array of objects that do not allow duplicate values ,equals Method determines the address of the object ( quote ), No matter what the value is ,new The memory address of the object must be different , therefore equals Method cannot be judged , Need to rewrite .
Be careful : In rewriting equals Methods are also generally overridden hashCode Method .
No rewriting equals() when :
public class Equalstest {
public static void main(String[] args) {
// establish List list students The storage type is Students The elements of
List<Student> students = new ArrayList<>();
// Statement Student Type of student object , Note that it is a statement , Not created ;
Student student;
for(int i=0;i<10;i++){
// establish student object , Each iteration creates an object ;
student=new Student(" Xiao Ming ");
// Judge students Whether this... Is included in the list student, If not , Add to list
//contains() Method , Determine whether the gift box contains an element , With the return true, No return false;
// Be careful :contains() Source code used in equals() Method
if(!students.contains(student)){
students.add(student);
}
}
System.out.println(students.size());// Output 10
}
}
class Student{
private String name;
public Student(String name){
this.name=name;
}
}Rewrote equals() when :
public class Equalstest2 {
public static void main(String[] args) {
List<Student2> students=new ArrayList<>();
Student2 student;
for(int i=0;i<10;i++){
student=new Student2(" Xiao Ming ");
if(!students.contains(student)){
students.add(student);
}
}
System.out.println(students.size());//1
}
}
class Student2{
private String name;
public Student2(String name){
this.name=name;
}
public String getName(){
return name;
}
// rewrite equals Method
public boolean equals(Object obj){
if(obj==this){
return true;
}
if(obj instanceof Student2){
Student2 student = (Student2) obj;
if(student.getName().equals(this.name)){
return true;
}
}
return false;
}
}clone()
protected native Object clone() throws CloneNotSupportedException;
protected Method , Provided to subclass override , But it needs to be realized Cloneable Method , This is a tag interface , If not , When calling object.clone() Method , Will throw out CloneNotSupportedException.
Be careful : Use clone() Method must be overridden clone() Method .
toString()
public String toString();
public Method , Subclasses can override , default toString Method returns the fully qualified class name of the current class [email protected]+ Hexadecimal hashCode value .
How to rewrite : Write what you want to return .
public String toString() {
return "[name = " + getName() + ",age = "+ getAge() + "]";
}notify()
notifyAll()
wait(long)
wait(long,int)
wait()
These three methods are used for inter thread communication , The function is to block the current thread , Wait for another thread to call notify()/notifyAll() Method to wake it up . All three methods are public final Of , Cannot be rewritten .
finalize()
边栏推荐
- 加速訓練之並行化 tf.data.Dataset 生成器
- Interview must ask: summary of ten classic sorting algorithms
- C asynchronous programming (async and await) and asynchronous method synchronous invocation
- MySQL5.7.21 Build For ARM
- Betteland introduces milk products of non animal origin, which will be launched in the U.S. market in the near future
- Self implementation of a UI Library - UI core drawing layer management
- 2022 self study materials for Zhejiang computer level III network and security technology examination (1) (updated on 2.28)
- Ubunt 20.04 uses CDROM or ISO as the installation source
- Understanding of day16 array create query static and dynamic array array array performance in memory
- One dragon and one knight accompanying programmers are 36 years old
猜你喜欢

How to deploy PostgreSQL as a docker container

Gavin teacher's perception of transformer live class - rasa dialogue robot project practice in the field of education agency mode and core component source code analysis under the microservice of educ

National land use data of 30m precision secondary classification

Big manufacturers compete to join rust, performance and safety are the key, and the 2021 rust developer survey report is announced

SQL注入上传一句话木马(转)

Ten trends of Internet Security in 2022 industry released

L1-064 AI core code valued at 100 million (20 points)

Some problems of Qinglong panel

JWT learning and use

Pupanvr- an open source embedded NVR system (1)
随机推荐
Development of video preview for main interface of pupanvr-ui
Map coordinate conversion of Baidu map API
Harris corner detection principle-
Summary of problems in rv1109/rv1126 product development
How to deploy dolphin scheduler 1.3.1 on cdh5
kali_ Nat mode, bridging Internet / host only_ detailed
How to generate provincial data from county-level data in ArcGIS?
L1-068 harmonic average (10 points)
In the era of smart retail, Weimeng reshapes the value of "shopping guide"
Install pycharm under Kali and create a shortcut access
Musk promotes the development of fascinating new products partners remind important questions
Accumulated temperature spatial distribution data, temperature distribution data, sunshine data, rainfall distribution, solar radiation data, surface runoff data, land use data, NPP data, NDVI data
Musk promotes the development of fascinating new products partners remind important questions
Chapter 1
2022 "college entrance examination memory" has been packaged, please check!
22-2-28 there are many things to do at work today, ETH analysis
Difference between thread and task
InnoDB data storage structure – MySQL
2022“高考记忆” 已打包完成,请查收!
IC验证中的force/release 学习整理(6)研究对 wire 类型信号的影响