三. 面向对象
-
Object类
-
查找Object类中的方法:源码/Java类库帮助文档
-
protected Object clone() //克隆对象
int hashCode() //获取对象哈希值
boolean equals(Object dbj) //判断两个对象是否相等
String toString() //将对象转换成字符串形式
protected void finalize() //垃圾回收期负责调用的方法
-
API(Application Program Interface):
应用程序编程接口,整个JDK类库就是一个javase的API,每个API都会配置一套API帮助文档
1. toString()方法
SUN建议所有子类重写toString()方法
public String toString() {
//toString()方法的默认实现:类名@对象的内存地址转换为十六进制字符串
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
public class ToStringTest01 {
public static void main(String[] args) {
MyTime t = new MyTime(2020,11,24);
System.out.println(t); //输出引用时,自动调用toString()方法
}
}
class MyTime{
public int year,month,day;
public MyTime(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public String toString(){
//重载前返回MyTime@1b6d3586,重载后输出2020年11月24日
return year + "年" + month + "月" + day + "日";
}
}
2. equals方法
Java中比较基本数据类型数据是否相等用“==”,比较引用数据类型统一用equals方法
public boolean equals(Object obj) {
return (this == obj);
}
- equals方法的目的:判断两个对象是否相等
public class EqualsTest01 {
public static void main(String[] args) {
int a = 10;
int b = 10;
System.out.println(a == b); //输出true
MyTime t1 = new MyTime(2020,11,25);
MyTime t2 = new MyTime(2020,11,25);
//“==”判断两个对象内存地址是否相同
System.out.println(t1 == t2); //输出false
boolean x1 = t1.equals(t2); //重写equals方法前输出false
System.out.println(x1); //重写equals方法后输出true
MyTime t3 = null;
System.out.println(t1.equals(t3)); //输出false,未出现空指针异常
}
}
class MyTime{
public int year,month,day;
public MyTime(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
//覆盖equals方法,当引用所指向的对象保存数据相同时输出true:
public boolean equals(Object obj){
if (obj instanceof MyTime){
MyTime t = (MyTime) obj;
if (year == t.year && month == t.month && day == t.day){
return true;
}
}return false;
}
}
-
String类的toString()方法和equals方法已经重写
比较字符串时不能用“==”,必须用equals方法
public class StringTest01 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2); //false
System.out.println(s1.equals(s2)); //true
System.out.println(s1.toString()); //输出hello
System.out.println(s1); //输出引用时自动调用toString()方法
String a = "x";
String b = "x";
System.out.println(a == b); //true
}
}
public class EqualsTest01 {
public static void main(String[] args) {
Student s1 = new Student(001,"蓝翔");
Student s2 = new Student(001,"蓝翔");
System.out.println(s1 == s2); //false
System.out.println(s1.equals(s2)); //true
}
}
class Student{
int no;
String school;
public Student(int no, String school) {
this.no = no;
this.school = school;
}
//覆盖toString()方法
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || !(obj instanceof Student)) return false;
Student s = (Student) obj;
//判断字符串是否相同时调用equals方法
return no == s.no && this.school.equals(s.school);
}
}
- equals方法多层重写
public class EqualsTest02 {
public static void main(String[] args) {
User u1 = new User("张三",new Address("北京","西城"));
User u2 = new User("张三",new Address("北京","西城"));
System.out.println(u1.equals(u2));
}
}
class User{
String name;
Address addr;
public User(String name,Address addr){
this.name = name;
this.addr = addr;
}
//如果两个对象姓名、住址相同,表示两个对象相同
public boolean equals(Object obj){
if (obj == null || !(obj instanceo