Object类中的方法及作用详解

一、引言

Java Object 类是所有类的父类,也就是说 Java 的所有类都继承了 Object,子类可以使用 Object 的所有方法。Object 类位于 java.lang 包中,编译时会自动导入,我们创建一个类时,如果没有明确继承一个父类,那么它就会自动继承 Object,成为 Object 的子类。

二、Object方法详解

通过阅读源码可知Object中含有:Object()、getClass()、hashCode()、equals()、clone()、toString()、notify()、notifyAll()、wait(long)、wait(long,int)、wait()、finalize()共十二个方法。

对这些方法功能进行简单的描述:

1. Object():默认构造方法

 public Object() {
        throw new RuntimeException("Stub!");
    }

2. clone():创建并返回此对象的一个副本

   @RecentlyNonNull
    protected Object clone() throws CloneNotSupportedException {
        throw new RuntimeException("Stub!");
    }

3. equals():指示某个其他对象是否与此对象相等

 public boolean equals(@Nullable Object obj) {
        throw new RuntimeException("Stub!");
    }

4. finalize():当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法

    protected void finalize() throws Throwable {
        throw new RuntimeException("Stub!");
    }

5. getClass():返回一个对象的运行时类

    @NonNull
    public final Class<?> getClass() {
        throw new RuntimeException("Stub!");
    }

6. hashCode():返回该对象的哈希值

    public int hashCode() {
        throw new RuntimeException("Stub!");
    }

7. notify():唤醒此对象监视器上等待的单个线程

    public final native void notify();

8. notifyAll():唤醒此对象监视器上等待的所有线程

    public final native void notifyAll();

9. toString():返回该对象的字符串表示

    @NonNull
    public String toString() {
        throw new RuntimeException("Stub!");
    }

10. wait():导致当前的线程等待,直到其它线程调用此对象的notify()或notifyAll()

    public final void wait() throws InterruptedException {
        throw new RuntimeException("Stub!");
    }

11. wait(long timeout):导致当前的线程等待调用此对象的notify()或notifyAll()

    public final void wait(long timeout) throws InterruptedException {
        throw new RuntimeException("Stub!");
    }

12. wait(long timeout, int nanos):导致当前的线程等待,直到其他线程调用此对象的notify()或notifyAll(),或其他某个线程中断当前线程,或者已超过某个实际时间量

    public final native void wait(long var1, int var3) throws InterruptedException;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

多仔百事宅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值