目录
Object类概述
object类是老祖宗类,所有类直接或间接继承于该类。
Object类的hashCode()方法
返回该对象的哈希码值,根据对象的地址来计算。
同一个对象的哈希码值相同,不同对象的哈希码值不同
不是对象的实际地址值,可以理解为逻辑地址值。
public class MyTest {
public static void main(String[] args) {
Student s1 = new Student();
int i = s1.hashCode();
Student s2 = new Student();
int i2 = s2.hashCode();
System.out.println(i);
System.out.println(i2);
System.out.println(i==i2);
}
}
结果:
1163157884
1956725890
false
Object的getClass()方法
当字节码文件加载进内存,.class文件再创建一个对象
getClass() 获取字节码文件对应的对象,返回一个Class对象,这个对象保存着原对象的类信息
如Object.class 对应一个对象,Class类型是描述这个字节码文件类型的。
不同对象的地址值是不同的,但它们的getClass对应的对象是相同的
public class MyTest {
public static void main(String[] args) {
Student s1 = new Student();
Class<? extends Student> aClass = s1.getClass();
Student s2 = new Student();
Class<? extends Student> aClass1 = s2.getClass();
System.out.println(aClass);
System.out.println(aClass1);
System.out.println(aClass==aClass1);
}
}
结果:
class org.slxy.Demo.Student
class org.slxy.Demo.Student
true
Object的toString()方法
返回该对象的地址值,以字符串形式返回
打印对象名时,默认调用toString方法
若调用对象名时,不想打印地址值,可以对该类的toString方法进行重写,来获取自己想要的信息。
public class MyTest {
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(s1);
System.out.println(s1.toString());
}
}
结果:
org.slxy.Demo.Student@4554617c
org.slxy.Demo.Student@4554617c
可见,地址值相同。
对toString方法进行重写
public class Student {
String name = "张三";
int age = 20;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
获得的结果为:
Student{name='张三', age=20}
Student{name='张三', age=20}
Object的equals()方法
判断两个对象是否相等,和==判断相同,比较的是对象的引用,即地址值
Student s1 = new Student();
Student s2 = new Student();
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
结果:
false
false
因为比较对象没有意义,一般重写该方法,比较成员变量的值是否相等。
public class Student {
private String name;
private int 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;
}
public Student(){
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Student)) {
return false;
}
Student stu = (Student) obj;
return this.name.equals(stu.name) && this.age == stu.age;
}
}
优化代码,提高效率
public class MyTest {
public static void main(String[] args) {
Student s1 = new Student("张三",20);
Student s2 = new Student("张三",20);
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
}
}
instanceof 判断该对象是不是一个类的对象
Object类的clone()方法
克隆对象,返回该对象的一个副本。
clone()是被protected 修饰,意思是受保护的,若对象要在其他类克隆,必须重写对象对应的类的clone()方法,把该方法的权限修饰符改为public ,采用的是浅克隆。克隆对象对应的类要实现一个Cloneable 这个标记接口(没有任何方法),才能完成克隆操作。
public class MyTest {
public static void main(String[] args) throws CloneNotSupportedException {
Dog dog = new Dog("小白",3);
Dog cloneDog = (Dog)dog.clone();
cloneDog.name = "旺财";
cloneDog.age = 5;
System.out.println(cloneDog.name);
System.out.println(cloneDog.age);
}
}
class Dog implements Cloneable{
String name;
int age;
public Dog(String name,int age) {
this.name = name;
this.age = age;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
对象的浅克隆就是克隆一个对象的时候,如果被克隆的对象中维护了另外一个类的对象,这时候只是克隆另外一个对象的地址,而没有把另外一个对象也克隆一份。
public class MyTest {
public static void main(String[] args) throws CloneNotSupportedException {
DogFood dogFood = new DogFood("王中王");
Dog dog = new Dog("小白",3,dogFood);
//设置名字
dog.dogFood.foodName = "双汇";
//克隆狗对象
Dog cloneDog = (Dog)dog.clone();
cloneDog.dogFood.foodName = "肉罐头";
System.out.println(dog.dogFood.foodName);
System.out.println(cloneDog.dogFood.foodName);
}
}
class Dog implements Cloneable{
String name;
int age;
DogFood dogFood;
public Dog(String name,int age,DogFood dogFood) {
this.name = name;
this.age = age;
this.dogFood = dogFood;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class DogFood {
String foodName;
public DogFood(String foodName) {
this.foodName = foodName;
}
}