Object类

时间:2021-06-30 18:09:41   收藏:0   阅读:0

一.Object类

1.toString

一般子类都有覆盖。默认返回:对象的 class 名称 + @ + hashCode 的十六进制字符串。

public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }

2.getClass

获取class 对象

public final native Class<?> getClass();

3.hashCode

获取对象的哈希值

public native int hashCode();

4.equals

比较两个对象是否相等,默认比较两个对象的地址

public boolean equals(Object obj) { return (this == obj);}

5.wait

释放锁,并让当前线程进入等待状态,直到其他线程调用此对象的 notify方法或 notifyAll 方法,当前线程被唤醒,进入“就绪状态”
public final void wait() throws InterruptedException { wait(0);}

6.wait(long timeout)

释放锁,并让当前线程进入等待状态,超过等待时间,当前线程被唤醒,进入“就绪状态”
public final native void wait(long timeout) throws InterruptedException;

7.wait(long timeout, int nanos)

增加了纳秒时间,释放锁,并让当前线程进入等待状态,超过等待时间,当前线程被唤醒,进入“就绪状态”
public final void wait(long timeout, int nanos) throws InterruptedException {
  if (timeout < 0) {
    throw new IllegalArgumentException("timeout value is negative");
  }

  if (nanos < 0 || nanos > 999999) {
    throw new IllegalArgumentException(
              "nanosecond timeout value out of range");
  }

  if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
    timeout++;
  }

  wait(timeout);
}

8.notify

唤醒在该对象上等待的某个线程

public final native void notify();

9.notifyAll

唤醒在该对象上等待的所有线程

public final native void notifyAll();

10.clone

实现对象的复制,成员对象只会拷贝引用地址,不会重新创建新对象

protected native Object clone() throws CloneNotSupportedException;

11.finalize

重写了,垃圾回收器回收对象时,会先调用此方法

protected void finalize() throws Throwable { }

 

二.重写equals方法,得重写hashCode方法

 

三.浅拷贝和深拷贝

 

评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!