Object类是所有java类的父类。
用户定义了如下一个Person类
public class Person{}
在类定义中并没有明确继承Object类,但是编译器会自动的完成这个过程。
既然所有类都继承自Object,那么它所具有的方法一定很重要。接下来就看看Object所具有的一些方法。
1 public class Object { 2 public final native Class<?> getClass(); 3 public native int hashCode(); 4 public boolean equals(Object obj) { 5 return (this == obj); 6 } 7 protected native Object clone() throws CloneNotSupportedException; 8 public String toString() { 9 return getClass().getName() + "@" + Integer.toHexString(hashCode()); 10 } 11 public final native void notify(); 12 public final native void notifyAll(); 13 public final native void wait(long timeout) throws InterruptedException; 14 public final void wait(long timeout, int nanos) throws InterruptedException { 15 if (timeout < 0) { 16 throw new IllegalArgumentException("timeout value is negative"); 17 } 18 if (nanos < 0 || nanos > 999999) { 19 throw new IllegalArgumentException( 20 "nanosecond timeout value out of range"); 21 } 22 if (nanos >= 500000 || (nanos != 0 && timeout == 0)) { 23 timeout++; 24 } 25 wait(timeout); 26 } 27 public final void wait() throws InterruptedException { 28 wait(0); 29 } 30 }