Java Object类

来源:互联网 发布:苹果6网络不可用 编辑:程序博客网 时间:2024/05/29 19:37

众所周知,Object类是Java所有类的父类,所有的类(Object除外)都默认继承了Object类。根据继承的特点,父类的public方法可以在子类中使用,由此想来,作为万类之父的Object,所设计的方法应该是具有普遍性的。我翻阅了一下Object类的API和Object类的源码,概括了一下,Object类一些方法。

Object 类的源码:(注释已经去掉了)

package java.lang;public class Object {    private static native void registerNatives();    static {        registerNatives();    }    public final native Class<?> getClass();     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());    }     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 {        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);    }    public final void wait() throws InterruptedException {        wait(0);    }    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 ;
public final void wait() throws InterruptedException ;

这些方法的底层都是native方法,是对底层线程的操作的方法。


然后是我们比较常用的一些方法:

 public final native Class<?> getClass();  反射的时候用到
 public boolean equals(Object obj) ;  比较两个对象是否相等,经常用到
 public String toString() ; 常用的toString()方法


还有一些方法,似乎很少用到:

public native int hashCode();返回一个对象的哈希码

protected native Object clone() throws CloneNotSupportedException;  克隆对象
protected void finalize() throws Throwable ;关于java垃圾收集器的方法




0 0