Files
javaSE260715/s-day01/src/com/inmind/object01/Demo03.java
T
2026-07-28 15:22:04 +08:00

33 lines
1.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.inmind.object01;
/*
Object类的equals方法:
boolean equals(Object obj) 指示一些其他对象是否等于本对象(判断对象内容)
==
基本数据类型:比较2个数据值是否相同
引用数据类型:比较的是地址是否相同
在实际开发中,地址值对我们没有太大的作用,比较关心数据内容,经常将内容相同的2个对象,看作是同一个,要使用Object的equals方法
Object父类的源码:
public boolean equals(Object obj) {
return (this == obj);
}
如果父类的内容比较功能,不符合子类(Student)的需求,那就重写equals方法
*/
public class Demo03 {
public static void main(String[] args) {
String s1 = "123";
String s2 = "123";
System.out.println(s1 == s2);// true
//创建2个学生对象(内容相同的2个对象,看作是同一个)
Student stu1 = new Student("张三", 18);
Student stu2 = new Student("张三", 18);
System.out.println(stu1 == stu2);//false
System.out.println(stu1.equals(stu2));//true!
}
}