当前位置:网站首页>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 .
边栏推荐
- OPENGL学习(二)OPENGL渲染管线
- Inoic4 learning notes 2
- Mysql——》数据类型隐式转换
- The assignment and answer of the "Cyberspace Security" competition of the 2020 secondary vocational group in Zhejiang Province (flag)
- Convolutional Neural Networks in TensorFlow quizs on Coursera
- OPENGL学习(三)GLUT二维图像绘制
- OpenGL learning (IV) glut 3D image rendering
- 投资的新阶段
- ETL development tool kettle download installation environment construction and use tutorial
- [question 39] special question for Niuke in-depth learning
猜你喜欢

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

MySQL - bufferpool related information

FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(下)

L4L7负载均衡

2022 Hangdian multi school second session 1009 shuangq (Mathematics)

全国职业院校技能大赛网络安全竞赛——Apache安全配置详解

Go Xiaobai implements a simple go mock server

OPENGL学习(二)OPENGL渲染管线

Getting started with MySQL database

Type-C PD protocol chip while charging and listening
随机推荐
Valentine's Day gift ----- use her photos and our chat records to generate word clouds~
core dump
Ionic4 learning notes 4 -- add a tab page
Type-C PD protocol chip while charging and listening
使用der格式公钥生成publicKey报错
QT - animation frame
2020年中职组“网络空间安全”赛项浙江省竞赛任务书及答案(Flag)
Those gods on Zhihu reply
vim相关介绍
Ionic4 learning notes 1
深度学习中Dropout原理解析
多线程与并发编程常见问题(未完待续)
matplotlib
Serial vector format (SVF) file format
Ionic4 learning notes 8 -- UI component 2 list (no practice, direct excerpt)
Vsftpd2.3.4 port penetration 6200 IRC_ 3281_ backdoor
Cadence OrCAD capture tcl/tk script example
Mysql数据库,去重,连接篇
永恒之蓝MS17-010exp复现
[question 39] special question for Niuke in-depth learning