Object类 常用方法

object类 常用方法

目录

  • object类 常用方法
    • 一、getClass()方法
    • 二、hashCode()方法
    • 三、toString()方法
    • 四、equals()方法
    • 五、finalize()方法
    • 源代码

一、getClass()方法

public final class getClass(){ }

  1. 返回引用中存储的实际对象类型
  2. 通常用于判断两个引用中实际存储对象类型是否一致。

代码示例

public class Test {
    public static void main(String[] args) {
        Student jordan = new Student("jordan", 182);
        Student susan = new Student("susan", 165);
        Syytem.out.println(jordan.getClass());
        Class class1 = jordan.getClass();
        Class class2 = susan.getClass();
        if (class1 == class2){
            System.out.println("jordan和susan为同一类型");
        }else System.out.println("jordan和susan不为同一类型");
    }
}
//运行结果
class com.cclass.DemoGetclass.Student
jordan和susan为同一类型

Process finished with exit code 0

二、hashCode()方法

public int hashCode(){ }

  1. 返回该对象的哈希码值。
  2. 哈希值根据对象的地址、字符串或数字使用hash算法计算出来的int类型的数据。
  3. 一般情况下相同对象返回相同hash码。

代码示例

public class Test {
    public static void main(String[] args) {
        Student jordan = new Student("jordan", 182);
        Student susan = new Student("susan", 165);
        System.out.println(jordan.hashCode());
        System.out.println(susan.hashCode());
    }
}
//运行结果
21685669
2133927002
Process finished with exit code 0

三、toString()方法

public String toString(){ }

  1. 返回该对象的字符串表达形式
  2. 可以根据程序需求 重写(覆盖) 该方法,如:展示对象各个属性值。

代码示例

public class Test {
    public static void main(String[] args) {
        Student jordan = new Student("jordan", 182);
        Student susan = new Student("susan", 165);
        System.out.println(jordan.toString());
        System.out.println(susan.toString());
    }
}
//运行结果
com.cclass.DemoGetclass.Student@14ae5a5
com.cclass.DemoGetclass.Student@7f31245a  //16进制
Process finished with exit code 0

重写示例

@Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", height=" + height +
                '}';
    }
//运行结果
Student{name='jordan', height=182}
Student{name='susan', height=165}

Process finished with exit code 0

四、equals()方法

public boolean equals(Object obj){ }

  1. 默认实现为(this == obj)比较两个对象地址是否相同
  2. 可进行重写(覆盖),比较两个对象的内容是否相同。
  • 比较两个引用是否指向同一个对象。
  • 判断obj是否为null。
  • 判断两个引用的实际对象类型是否一致。
  • 强制转换
  • 依次比较各个属性是否相同。

代码示例

public class Test {
    public static void main(String[] args) {
        Student jordan = new Student("jordan", 182);
        Student susan = new Student("susan", 165);
        System.out.println(jordan.equals(susan));
    }
}
//运行结果
false

Process finished with exit code 0

重写覆盖示例

@Override
    public boolean equals(Object obj) {
        if (this == obj){
            return true;
        }
        if (this == null){
            return false;
        }
        if (obj instanceof Student){
            //强制转换 将object转为Student 大--->小
            Student s = (Student) obj;
            if (this.name.equals(s.getName()) && this.height == s.getHeight()){
                return true;
            }
        }
        return false;
    }
//测试类如下
public class Test {
    public static void main(String[] args) {
        Student jordan = new Student("jordan", 182);
        Student susan = new Student("susan", 165);
        System.out.println(jordan.equals(susan));
        Student susan1 = new Student("susan", 165);
        System.out.println(susan1.equals(susan));
    }
}

//输出结果
false
true

Process finished with exit code 0

五、finalize()方法

  1. 当对象被判定为垃圾对象时,由JVM自动调用此方法,用以标记垃圾对象,进入回收队列。
  2. 垃圾对象是指没有有效引用指向此对象时,为垃圾对象。
  3. 垃圾回收:由GC销毁垃圾对象,释放数据存储空间。
  4. 自动回收机制:JVM的内存耗尽时,一次性回收所有垃圾对象。
  5. 手动回收机制:使用System.gc(); 通知JVM进行垃圾回收。

代码示例

//重写finalize
@Override
    protected void finalize() throws Throwable {
        System.out.println(this.name+"被垃圾回收了");
    }
//测试类
public class Test {
    public static void main(String[] args) {
        new Student("aaa",19);
        new Student("bbb",19);
        new Student("ccc",19);
        System.gc();
    }
}
//运行结果
ccc被垃圾回收了
bbb被垃圾回收了
aaa被垃圾回收了

Process finished with exit code 0

源代码

//Student类
package com.cclass.DemoGetclass;

public class Student {
    private String name;
    private int height;

    public Student(String name, int height) {
        this.name = name;
        this.height = height;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", height=" + height +
                '}';
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj){
            return true;
        }
        if (this == null){
            return false;
        }
        if (obj instanceof Student){
            //强制转换 将object转为Student 大--->小
            Student s = (Student) obj;
            if (this.name.equals(s.getName()) && this.height == s.getHeight()){
                return true;
            }
        }
        return false;
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println(this.name+"被垃圾回收了");
    }
}
//Test 类
package com.cclass.DemoGetclass;

public class Test {
    public static void main(String[] args) {
        Student jordan = new Student("jordan", 182);
        Student susan = new Student("susan", 165);
        Class class1 = jordan.getClass();  //.getClass() 用于判断两个类是否为同一类型
        Class class2 = susan.getClass();
        if (class1 == class2){
            System.out.println("jordan和susan为同一类型");
        }else System.out.println("jordan和susan不为同一类型");
        System.out.println("========================");
        System.out.println(jordan.hashCode());  //.hashCode() 返回该对象的哈希码值
        System.out.println(susan.hashCode());
        System.out.println("========================");
        System.out.println(jordan.toString());
        System.out.println(susan.toString());
        System.out.println("========================");
        System.out.println(jordan.equals(susan));
        Student susan1 = new Student("susan", 165);
        System.out.println(susan1.equals(susan));
        System.out.println("========================");
        new Student("aaa",19);
        new Student("bbb",19);
        new Student("ccc",19);
        System.gc();
    }
}

//运行结果
jordan和susan为同一类型
========================
21685669
2133927002
========================
Student{name='jordan', height=182}
Student{name='susan', height=165}
========================
false
true
========================
ccc被垃圾回收了
bbb被垃圾回收了
aaa被垃圾回收了

Process finished with exit code 0

你可能感兴趣的:(java)