25-Object类

Object类

  • 超类、基类,所有类的直接或间接父类,位于继承树的最顶层
  • 任何类,若没有写extends显示继承某个类,都默认直接继承Object类,否则为间接继承
  • Object类中所定义的方法,是所有对象都具备的方法
  • Object类型可以存储任何对象
  1. 作为参数,可接收任何对象
  2. 作为返回值,可返回任何对象

getClass()方法

  • public final Class<?> getClass(){}
  • 返回引用中存储的实际对象类型
  • 应用:通常用于判断两个引用中实际存储对象类型是否一致
public class Student {
    private String name;
    private int age;

    public Student() {
    }
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
    public static void main(String[] args) {
        Student s1 = new Student("aaa",20);
        Student s2 = new Student("bbb",22);
        //判断s1和s2是不是同一个类型
        Class class1 = s1.getClass();//后面讲反射的时候,会重点学习Class类
        Class class2 = s2.getClass();
        if(class1==class2){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }//输出yes
    }

hashCode()方法

  • pubilc int hashCode(){}
  • 返回该对象的哈希值
  • 哈希值根据对象的地址或字符串或数字使用hash算法计算出来的int类型的数值。
  • 一般情况下相同对象返回相同哈希码
    public static void main(String[] args) {
        Student s1 = new Student("aaa",20);
        Student s2 = new Student("bbb",22);
        
        //hashCode方法
        System.out.println(s1.hashCode());//460141958
        System.out.println(s2.hashCode());//1163157884
        Student s3 = s1;
        System.out.println(s3.hashCode());//460141958
    }

toString()方法

  • public String toString(){}
  • 返回该对象的字符串表示(表现形式:对象的类型和16进制的哈希值)
  • 可以根据程序需求重写该方法,如:展示对象的各个属性值
在Student类中重写toString方法//默认输出对象类型@16进制哈希值
        //重写toString方法
    @Override
    public String toString() {
        return name+":"+age;
    }
        //toString方法
        System.out.println(s1.toString());//com.normalclass.demo01.Student@1b6d3586
        System.out.println(s2.toString());//com.normalclass.demo01.Student@4554617c
        //hash值和hashCode是一样的,toString是16进制的
        //想要得到属性,重写toString方法
        /*
        重写后输出:
        aaa:20
        bbb:22
         */

equals()方法

  • 可以比较两个对象是否相等
  • public boolean equals(Object obj){}
  • 默认实现为(this == obj)当前对象与传入对象,比较两个对象地址是否相同
  • 可以进行重写,比较两个对象的内容是否相同
        //equals方法
        System.out.println(s1.equals(s2));//false

        Student s4 = new Student("小明", 17);
        Student s5 = new Student("小明", 17);
        System.out.println(s4.equals(s5));//false:地址不一样(默认比较地址)

equals重写步骤

  1. 比较两个引用是否指向同一个对象
  2. 判断obj是否为null
  3. 判断两个引用指向的实际类型是否一致
  4. 强类型转换
  5. 依次比较各个属性值是否相同
    //重写equals

    @Override
    public boolean equals(Object obj) {
        //1.是否同一个引用
        if(this == obj){
            return true;
        }
        //2.obj是否null
        if (obj == null){
            return false;
        }
        //3.是否同一个类型
//        if(this.getClass() == obj.getClass()){
//        }
        if (obj instanceof Student){//instanceof判断对象是否是某种类型
            //4.强转
            Student s=(Student) obj;
            //5.比较属性
            if (this.name.equals(s.getName())&&this.age==s.getAge()){
                return true;
            }
        }

        return false;
    }
       //equals方法
        System.out.println(s1.equals(s2));//false

        Student s4 = new Student("小明", 17);
        Student s5 = new Student("小明", 17);
        System.out.println(s4.equals(s5));//重写后true

finalize()

  • 当对象被判定为垃圾对象时,由JVM自动调用此方法,用以标记垃圾对象,进入回收队列。(程序员自己不用)
  • 垃圾对象:没有有效引用指向此对象时,为垃圾对象
  • 垃圾回收:由GC销毁垃圾对象,释放数据存储空间
  • 自动回收机制:JVM内存耗尽,一次性回收所有垃圾对象
  • 手动:使用System.gc();通知JVM执行回收
        //重写回收,使其显示信息
    @Override
    protected void finalize() throws Throwable {
        System.out.println(this.name+"对象被回收了");
    }
        //手动finalize方法
        new Student("ccc",20);//没有引用指向对象
        System.gc();//ccc对象被回收了
posted @ 2024-06-25 16:38  呆头尖瓜  阅读(1)  评论(0编辑  收藏  举报