Object类的作用
Object类是Java中所有类的父类,所以,Java中所有类的对象都可以直接使用Object类中提供的一些方法
Object类的常见方法
方法名 | 说明 |
---|---|
public String toString() | 返回对象的字符串表示形式 |
public boolean equals(Object o) | 判断两个对象是否相等 |
protected Object clone() | 对象克隆 |
案例演示
toString()和equals()
Student类
package com.ligemanyin._Object;import java.util.Objects;/*** ClassName:Student* Package:com.ligemanyin._Object* Description:Object常用API演示** @Author:离歌慢饮* @CreateTime:2024/2/17 15:49* @Version:1.0*/
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;}@Overridepublic String toString() {return "[姓名:" + name + ",年龄:" + age + "]";}@Overridepublic boolean equals(Object o) {//判断两个对象地址是否一样,一样则返回trueif (this == o) return true;//判断o如果是null或比较者与被比较者的类型不一样,返回falseif (o == null || getClass() != o.getClass()) return false;//o不为null且o一定是Student对象,则将o转为Student类型并开始比较内容Student student = (Student) o;return age == student.age && Objects.equals(name, student.name);}
}
测试类StudentTest
package com.ligemanyin._Object;/*** ClassName:StudentTest* Package:com.ligemanyin._Object* Description:Object中的toString()和equals()方法演示** @Author:离歌慢饮* @CreateTime:2024/2/17 15:51* @Version:1.0*/
public class StudentTest {public static void main(String[] args) {Student student = new Student("张三", 22);System.out.println(student);Student student1 = student;Student student2 = new Student("李四", 23);System.out.println(student1); //[姓名:张三,年龄:22]System.out.println(student2); //[姓名:李四,年龄:23]System.out.println(student.equals(student1)); //trueSystem.out.println(student.equals(student2)); //falseStudent student3 = new Student("李四", 23);System.out.println(student2.equals(student3)); //true 根据重写的equals方法判断,其地址并不相同}
}
toString存在的意义: toString()方法存在的意义就是为了被子类重写,以便返回对象具体的内容
equals存在的意义: 直接比较两个对象的地址是否相同完全可以用“==”替代equals,equals存在的意义就是为了被子类重写,以便子类自己来制定比较规则(如比较对象内容)
clone()
User类
package com.ligemanyin._Object;import java.util.Arrays;/*** ClassName:User* Package:com.ligemanyin._Object* Description:Object中clone()方法演示** @Author:离歌慢饮* @CreateTime:2024/2/17 16:22* @Version:1.0*/
public class User implements Cloneable{ //Cloneable是一个标记接口,如果要实现clone功能,需实现该接口private int id; //编号private String userName; //用户名private String password; //密码private double[] scores; //分数public User() {}public User(int id, String userName, String password, double[] scores) {this.id = id;this.userName = userName;this.password = password;this.scores = scores;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public double[] getScores() {return scores;}public void setScores(double[] scores) {this.scores = scores;}@Overrideprotected Object clone() throws CloneNotSupportedException {//super去调用父类Object中的clone方法return super.clone();}@Overridepublic String toString() {return "User{" +"id=" + id +", userName='" + userName + '\'' +", password='" + password + '\'' +", scores=" + Arrays.toString(scores) +'}';}
}
测试类UserTest
package com.ligemanyin._Object;/*** ClassName:UserTest* Package:com.ligemanyin._Object* Description:** @Author:离歌慢饮* @CreateTime:2024/2/17 16:29* @Version:1.0*/
public class UserTest {public static void main(String[] args) throws CloneNotSupportedException {User u1 = new User(1, "zhangsan", "123456", new double[] {99.0,99});User u2 = (User) u1.clone();System.out.println(u1); //User{id=1, userName='zhangsan', password='123456', scores=[99.0, 99.0]}System.out.println(u2); //User{id=1, userName='zhangsan', password='123456', scores=[99.0, 99.0]}System.out.println(u1.equals(u2));System.out.println(u1 == u2); //克隆后会创建一个新对象出来,所以原对象与克隆对象地址不同System.out.println(u1.getScores()); //[D@67b6d4aeSystem.out.println(u2.getScores()); //[D@67b6d4ae//浅克隆中数组对象会直接拷贝其在堆内存中的地址,所以两个对象的数组地址才会相同}
}
浅克隆: 拷贝出的新对象,与原对象中的数据一模一样(引用类型拷贝的只是地址)
重写clone()
package com.ligemanyin._Object;import java.util.Arrays;/*** ClassName:User* Package:com.ligemanyin._Object* Description:Object中clone()方法演示** @Author:离歌慢饮* @CreateTime:2024/2/17 16:22* @Version:1.0*/
public class User implements Cloneable{ //Cloneable是一个标记接口,如果要实现clone功能,需实现该接口private int id; //编号private String userName; //用户名private String password; //密码private double[] scores; //分数public User() {}public User(int id, String userName, String password, double[] scores) {this.id = id;this.userName = userName;this.password = password;this.scores = scores;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public double[] getScores() {return scores;}public void setScores(double[] scores) {this.scores = scores;}@Overrideprotected Object clone() throws CloneNotSupportedException {//super去调用父类Object中的clone方法User u2 = (User) super.clone();//对数组对象进行克隆,即是将创建一个新的数组对象并将值拷贝给该对象,// 由此方法得到的数组对象与原数组对象不是同一个地址u2.scores = u2.scores.clone();return u2;}@Overridepublic String toString() {return "User{" +"id=" + id +", userName='" + userName + '\'' +", password='" + password + '\'' +", scores=" + Arrays.toString(scores) +'}';}
}
测试类UserTest
package com.ligemanyin._Object;/*** ClassName:UserTest* Package:com.ligemanyin._Object* Description:** @Author:离歌慢饮* @CreateTime:2024/2/17 16:29* @Version:1.0*/
public class UserTest {public static void main(String[] args) throws CloneNotSupportedException {User u1 = new User(1, "zhangsan", "123456", new double[] {99.0,99});User u2 = (User) u1.clone();System.out.println(u1); //User{id=1, userName='zhangsan', password='123456', scores=[99.0, 99.0]}System.out.println(u2); //User{id=1, userName='zhangsan', password='123456', scores=[99.0, 99.0]}System.out.println(u1.equals(u2));System.out.println(u1 == u2); //克隆后会创建一个新对象出来,所以原对象与克隆对象地址不同System.out.println(u1.getScores()); //[D@67b6d4aeSystem.out.println(u2.getScores()); //[D@34b7bfc0}
}
深克隆: 对象中基本类型的数据直接拷贝;对象中的字符串数据拷贝其地址;对象中还包含的其他对象,不会拷贝地址,而是创建新对象