Object 类研究

来源:互联网 发布:c语言课程设计题目 编辑:程序博客网 时间:2024/06/10 01:53

Object 类研究

概述

java中Object是所有对象的父类,上帝类。里面的方法我们应该对其深入掌握。


Object方法展示

代码展示:

public class Object {    private static native void registerNatives();    static {        registerNatives();    }    //获得类对应的Class对象    public final native Class<?> getClass();    //hashCode码,如不覆盖就调用操作系统本地的hashCode方法。    public native int hashCode();    //比较是否相等,默认直接比较是否引用同一个地址。    public boolean equals(Object obj) {        return (this == obj);    }    protected native Object clone() throws CloneNotSupportedException;    public String toString() {        return getClass().getName() + "@" + Integer.toHexString(hashCode());    }    /**     * Called by the garbage collector on an object when garbage collection     * determines that there are no more references to the object.     * A subclass overrides the  finalize method to dispose of     * system resources or to perform other cleanup.     */    //主动调用GC去回收改对象在jvm堆栈的垃圾    protected void finalize() throws Throwable { }    //以下几个方法只有在同步时获得对象锁时才可以调用,否则会报异常。    //唤醒一个线程    public final native void notify();    //唤醒持有锁对象的所有线程    public final native void notifyAll();    //放弃锁对象等待    public final native void wait(long timeout) throws InterruptedException;    //放弃锁对象等待(指定时间内放弃)       public final void wait(long timeout, int nanos) throws InterruptedException {        //省略入参校验        wait(timeout);    }     public final void wait() throws InterruptedException {        wait(0);//就是无限期sleep除非其它线程唤醒它。    }}

说明:注意哪些方法是final修饰不可被覆盖的,哪些方法是调用本地方法的。

0 0
原创粉丝点击