温馨提示×

java的object类的方法有哪些

小亿
82
2024-09-13 15:08:24
栏目: 编程语言

Java中的Object类是所有Java类的基类,它包含了一些公共的方法,这些方法可以被所有的Java对象使用。以下是Object类中的主要方法:

  1. public final Class<?> getClass(): 返回当前对象运行时的类。
  2. public int hashCode(): 返回该对象的哈希码值。
  3. public boolean equals(Object obj): 指示其他某个对象是否与此对象“相等”。
  4. protected Object clone() throws CloneNotSupportedException: 创建并返回此对象的一个副本。
  5. public String toString(): 返回该对象的字符串表示。
  6. public final void notify(): 唤醒在此对象监视器上等待的单个线程。
  7. public final void notifyAll(): 唤醒在此对象监视器上等待的所有线程。
  8. public final void wait(long timeout) throws InterruptedException: 导致当前的线程等待,直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者经过指定的时间量。
  9. public final void wait(long timeout, int nanos) throws InterruptedException: 导致当前的线程等待,直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者其他某个线程中断当前线程,或者已过某个实际时间量。
  10. public final void wait() throws InterruptedException: 导致当前的线程等待,直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法。

注意,wait(), notify(), 和 notifyAll() 方法只能在同步代码块或同步方法中使用,否则会抛出 IllegalMonitorStateException

0