当前位置:网站首页>Equals() method of object class
Equals() method of object class
2022-07-24 18:53:00 【Programming Altman】
package itheima_02;
/*
Test class
*/
public class ObjectionDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName(" Brigitte Lin ");
s1.setAge(30);
Student s2 = new Student();
s2.setName(" Brigitte Lin ");
s2.setAge(30);
// demand , Compare the contents of two objects
// System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
/* public boolean equals(Object obj) { // this --- s1 //obj --- s2 return (this == obj); }*/
}
}
package itheima_02;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
/* this --- s1 o ---s2 */
// Compare the addresses , If the same , Go straight back to true
if (this == o) return true;
// Judge whether the parameter is null
// Determine whether two objects come from the same class
if (o == null || getClass() != o.getClass()) return false;
// Move down
Student student = (Student) o;// student = s2;
// Compare the same age
if (age != student.age) return false;
// Compare whether the contents of the names are the same
return name != null ? name.equals(student.name) : student.name == null;
}
}
public String to String() Returns the string representation of the object . It is recommended that all subclasses override this method , Automatic generation .
public boolean equals(Object obj) Compare objects for equality , Default comparison address , Rewriting can compare content , Automatic generation .
边栏推荐
- Ionic4 learning notes 1
- OPENGL学习(四)GLUT三维图像绘制
- Ionic4 learning notes 7 -- UI component 1 (no practice, direct excerpt)
- [Tkinter] common components (II)
- Valentine's Day gift ----- use her photos and our chat records to generate word clouds~
- 理解corners_align,两种看待像素的视角
- TCL programming style guide
- ETL开发工具Kettle下载安装环境搭建及使用教程
- Wechat applet reverse
- Convolutional Neural Networks in TensorFlow quizs on Coursera
猜你喜欢

轻松学Pytorch-迁移学习实现表面缺陷检查

04 distributed resource management system yarn

Vsftpd2.3.4-端口渗透 6200 irc_3281_backdoor

matplotlib

Go小白实现一个简易的go mock server

EasyUI framework dialog repeated loading problem

【历史上的今天】7 月 24 日:Caldera 诉微软案;AMD 宣布收购 ATI;谷歌推出 Chromecast

Analysis of dropout principle in deep learning

Inoic4 learning notes 2

Nftscan and port3 have reached strategic cooperation in the field of NFT data
随机推荐
32-bit stack overflow advanced
线程的生命周周期和基本方法
Ionic4 learning notes 4 -- add a tab page
杭电多校第一场第三题 Backpack(异或dp+bitset)
2022暑期杭电多校第一场1012Alice and Bob(博弈论)
Traversal and splicing of strings
FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(上)
OPENGL学习(五)Modern OpenGL 三角形绘制
OpenGL learning (III) glut two-dimensional image rendering
Easily learn pytoch transfer learning to realize surface defect inspection
2022 Summer Games of Hangzhou electric power multiple schools 1012aalice and Bob (game theory)
理解corners_align,两种看待像素的视角
Understand dynamic calculation diagram, requires_ grad、zero_ grad
FPGA 20 routines: 9. DDR3 memory particle initialization write and read through RS232 (Part 1)
Thread lifecycle and basic methods
ETL development tool kettle download installation environment construction and use tutorial
Principle and application of database
Create parent-child projects in clion (cmake tool) and introduce the method of third-party libraries
Wireshark simple filter rule
使用der格式公钥生成publicKey报错