Object类

Object类

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

getClass方法

  • public final Class<?> getClass(){}
  • 返回引用中存储的实际对象类型
package com.lin.object.demo01;

public class Test {
    public static void main(String[] args) {
        Student s1 = new Student("aaa", 18);
        Student s2 = new Student("bbb", 20);

        //判断s1,s2是不是同一个类型
        Class class1 = s1.getClass();
        Class class2 = s2.getClass();

        if(class1 == class2){
            System.out.println("同一个类型");
        }else{
            System.out.println("不是同一个类型");
        }
    }
}

hashCode()方法

  • public int hashCode() {}
  • 返回该对象的哈希值。
  • 哈希值根据对象的地址字符串数字使用hash算法计算出来的int类型的数值。
  • 一般情况下相同对象返回相同的哈希码。
package com.lin.object.demo02;

public class Test {
    public static void main(String[] args) {
        Student s1 = new Student("aaa", 18);
        Student s2 = new Student("bbb", 20);

        System.out.println(s1.hashCode());

        System.out.println(s2.hashCode());
        Student s3 = s1;
        System.out.println(s3.hashCode());
    }
}

toString方法

  • public String toString() {}
  • 返回该对象的字符串表示(表现形式)
  • 可以根据程序需求覆盖此方法
package com.lin.object.demo03;


public class Test {
    public static void main(String[] args) {
       Student s1 = new Student("aaa", 18);
       Student s2 = new Student("bbb", 20);

        //toString()方法
        System.out.println(s1.toString());
        System.out.println(s2.toString());
    }
}

/*重写前
*
* com.lin.object.demo03.Student@1b6d3586
* com.lin.object.demo03.Student@4554617c
* */

/*
重写后输出:
aaa,18
bbb,20
 */
package com.lin.object.demo03;

public class Student {

    private String name;
    private int age;

    public Student() {
    }

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

    //重写toString()方法
    @Override
    public String toString() {
        return name + "," + age;
    }
}

equals()方法

  • public boolean equals (Object obj)
  • 返回值 this == obj (比较的是两个对象的地址)
  • 可进行覆盖,比较两个对象的内容:
    • 比较两个引用是否指向同一个对象
    • 判断obj是否为null
    • 判断两个引用指向的实际对象类型是否一致
    • 强制类型转换
    • 依次比较各个属性值是否相同
package com.lin.object.demo04;

public class Test {

    public static void main(String[] args) {
        Student s1 = new Student("aaa", 18);
        Student s2 = new Student("bbb", 20);

        System.out.println(s1.equals(s2));

        Student s3 = new Student("小明", 18);
        Student s4 = new Student("小明", 18);
        System.out.println(s3.equals(s4));
    }

}

package com.lin.object.demo04;

public class Student {

    private String name;
    private int age;

    public Student() {
    }

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

    @Override
    public boolean equals(Object obj) {
        //1.比较两个对象是否是同一个引用
        if (this == obj) {
            return true;
        }
        //2.判断传递的对象是否为null
        if (obj == null) {
            return false;
        }
        //3.判断是否是同一个类型
        //instanceof关键字:判断对象是否是同种类型
        if (obj instanceof Student) {
            //4.向下转型,比较属性值
            Student s = (Student) obj;
            if (this.name.equals(s.name) && this.age == s.age) {
                return true;
            }
        }
        return false;
    }
}

finalize()方法

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

public class Test {

    public static void main(String[] args) {
//        Student s1 = new Student("aaa",18);
//        Student s2 = new Student("bbb",20);
//        Student s3 = new Student("aaa",18);
//        Student s4 = new Student("AAA",18);
//        Student s5 = new Student("AAA",18);

        new Student("aaa",18);
        new Student("bbb",20);
        new Student("aaa",18);
        new Student("AAA",18);
        new Student("AAA",18);
        //回收垃圾
        System.gc();
        System.out.println("回收垃圾");
    }

}

/*
输出:
垃圾被清扫了
垃圾被清扫了
垃圾被清扫了
垃圾被清扫了
垃圾被清扫了
回收垃圾
 */
@Override
    protected void finalize() throws Throwable {
        System.out.println("垃圾被清扫了");
    }
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值