建设一个网站需要注意哪些要求,建设商城类网站多少钱,班级优化大师官网下载,做民宿哪个网站好Object类的作用
Object类是Java中所有类的父类#xff0c;所以#xff0c;Java中所有类的对象都可以直接使用Object类中提供的一些方法
Object类的常见方法
方法名说明public String toString()返回对象的字符串表示形式public boolean equals(Object o)判断两个对象…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;/*** ClassNameStudent* Packagecom.ligemanyin._Object* DescriptionObject常用API演示** Author离歌慢饮* CreateTime2024/2/17 15:49* Version1.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;/*** ClassNameStudentTest* Packagecom.ligemanyin._Object* DescriptionObject中的toString()和equals()方法演示** Author离歌慢饮* CreateTime2024/2/17 15:51* Version1.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存在的意义 直接比较两个对象的地址是否相同完全可以用“”替代equalsequals存在的意义就是为了被子类重写以便子类自己来制定比较规则(如比较对象内容)
clone()
User类
package com.ligemanyin._Object;import java.util.Arrays;/*** ClassNameUser* Packagecom.ligemanyin._Object* DescriptionObject中clone()方法演示** Author离歌慢饮* CreateTime2024/2/17 16:22* Version1.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;/*** ClassNameUserTest* Packagecom.ligemanyin._Object* Description** Author离歌慢饮* CreateTime2024/2/17 16:29* Version1.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{id1, userNamezhangsan, password123456, scores[99.0, 99.0]}System.out.println(u2); //User{id1, userNamezhangsan, password123456, scores[99.0, 99.0]}System.out.println(u1.equals(u2));System.out.println(u1 u2); //克隆后会创建一个新对象出来所以原对象与克隆对象地址不同System.out.println(u1.getScores()); //[D67b6d4aeSystem.out.println(u2.getScores()); //[D67b6d4ae//浅克隆中数组对象会直接拷贝其在堆内存中的地址所以两个对象的数组地址才会相同}
}
浅克隆 拷贝出的新对象与原对象中的数据一模一样(引用类型拷贝的只是地址)
重写clone()
package com.ligemanyin._Object;import java.util.Arrays;/*** ClassNameUser* Packagecom.ligemanyin._Object* DescriptionObject中clone()方法演示** Author离歌慢饮* CreateTime2024/2/17 16:22* Version1.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;/*** ClassNameUserTest* Packagecom.ligemanyin._Object* Description** Author离歌慢饮* CreateTime2024/2/17 16:29* Version1.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{id1, userNamezhangsan, password123456, scores[99.0, 99.0]}System.out.println(u2); //User{id1, userNamezhangsan, password123456, scores[99.0, 99.0]}System.out.println(u1.equals(u2));System.out.println(u1 u2); //克隆后会创建一个新对象出来所以原对象与克隆对象地址不同System.out.println(u1.getScores()); //[D67b6d4aeSystem.out.println(u2.getScores()); //[D34b7bfc0}
}
深克隆 对象中基本类型的数据直接拷贝对象中的字符串数据拷贝其地址对象中还包含的其他对象不会拷贝地址而是创建新对象