Java Class - Object

Object 类是 Java 所有类的父类, 所有类默认都是继承这个类,不需要显示的指定 extends.

该类包含以下方法:

  1. public final native Class<?> getClass()

  2. public String toString()

  3. public final native void notify()

  4. public final native void notifyAll()

  5. public final void wait() throws InterruptedException

  6. public final native void wait(long timeout) throws InterruptedException

  7. public final void wait(long timeout, int nanos) throws InterruptedException

  8. public native int hashCode()

  9. public boolean equals(Object obj)

  10. protected native Object clone() throws CloneNotSupportedException

  11. protected void finalize() throws Throwable

 其中有些方法被声明为final, 也就是说不能被覆写。 在实际的编程中,我们可能会根据需要覆写其他的方法,比如 hashcode 和equal 等方法。

1) getClass

  - 获取运行时对象的Class实例

2) toString

  - 转换成字符串

3)  wait

  - 使当前线程进入等待状态,其他线程调用notify、notifyAll或者在指定的时间之后, 当前线程恢复到可运行状态

4) notify

  - 唤醒在这个对象上等待的线程

5) equal

  - 比较两个对象是否相等,默认实现是 ==

6) hashCode

  - 返回对象的哈希值,在使用哈希表的时候会用到

  在设计equal和hashCode的方法,要满足以下的关系 :

    两个对象equal方法相等,hashCode 一定相等

    两个对象不相等,hashcode可能相等

    两个对象hashcode相等,但是equal可能不相等

7) clone

  - 克隆对象, Object 的克隆方法是浅拷贝。如果要调用该方法,该类必须实现Cloneable接口。

    浅拷贝-基本类型被克隆一份,对于引用类型,只是拷贝引用

    深拷贝-克隆之后,完全不同的两个对象

8) finalize

  - 当对象需要被垃圾回收器回收的时候调用,用于清理资源

 

posted @ 2018-08-19 06:36  花落知少  阅读(138)  评论(0编辑  收藏  举报