Object是所有类的父类,它里面的方法被所有的类拥有,或者部分覆盖重写,因此了解Object中的方法,对理解其他类的方法是十分必要的。
//1.这个方法和static块结合使用,在类第一次被加载的时候,注册本地方法
private static native void registerNatives();
static {
registerNatives();
}
//2.运行时调用此方法返回对象所属的类,从而完成反射等一系列操作,此方法是final的,表示不想被子类覆盖修改
public final native Class<?> getClass();
//3.返回hashcode,任何一个对象都有此hashcode,如果对象相同equals,那么hashcode必须相同,但是hashcode相同,不一定是同一个对象equals
//因为object的equals方法比较的是引用,所以hashcode就是同一个对象的,肯定是相同的,但是如果重载的话,重载equals,则必须重载hashcode,
//使得equals则hashcode相同这个属性必须满足,才能完成集合当中的操作
public native int hashCode();
public boolean equals(Object obj) {
return (this == obj);
}
//每个对象对有clone方法,就是从根对象继承过来的
protected native Object clone() throws CloneNotSupportedException;
//为了方便代码的调试和输出,每个对象都有转化为字符串的方法,也就是toString,但是默认的toString是类名@hashcode()的十六进制值
//一般的类都会覆盖此方法然后实现自己特有格式的输出
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 > 0) {
timeout++;
}
wait(timeout);
}
public final void wait() throws InterruptedException {
wait(0);
}
//可以实现自己的finalize方法来完成一些清理动作
protected void finalize() throws Throwable { }