1. 程式人生 > >所有類的基類——Object

所有類的基類——Object

Object

public class Object
java.lang.Object

Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
Object類是類層次結構中的根,每個類的父類都是Object類。所有物件,包括陣列,都實現了這個類的方法。

Summary

Public constructors公共構造器

Object() ;

Public methods公共方法

return type(返回值) methods name(方法名) introduce(簡介)
boolean equals(Object obj) Indicates whether some other object is "equal to" this one.(判斷其他物件和這個物件是否相等)
final Class<?> getClass() Returns the runtime class of this Object.(返回執行時的類名)
int
hashCode() Returns a hash code value for the object. (返回次物件的雜湊程式碼值)
final void notify() Wakes up a single thread that is waiting on this object's monitor.(喚醒在該物件的監視器上等待的單個執行緒)
final void notifyAll() Wakes up all threads that are waiting on this object's monitor.(喚醒在該物件的監視器上等待的所有執行緒)
String toString() Returns a string representation of the object.(返回該物件的字串表達形式)
final void wait(long millis, int nanos) Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.(讓當前執行緒等待直到另一個執行緒呼叫了此物件的notify()notifyAll()方法,或者其他某個執行緒中斷了當前執行緒,或者過去了一定的時間)
final void wait(long millis) Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.(讓當前執行緒等待直到另一個執行緒呼叫了此物件的notify()notifyAll()方法,或者過去了一定的時間)
final void wait() Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.(讓當前執行緒等待直到另一個執行緒呼叫了此物件的notify()notifyAll()方法)

Protected methods

return type(返回值) methods name(方法名) introduce(簡介)
Object clone() Creates and returns a copy of this object.(建立並返回此物件的副本)
void finalize() Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.(當垃圾回收器確定該物件沒有被引用時,呼叫此方法)

Public methods方法簡介

equals

boolean equals (Object obj)

Indicates whether some other object is “equal to” this one.
判斷其他物件和這個物件是否相等。

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.

  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.

  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.

  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.

  • For any non-null reference value x, x.equals(null) should return false.

相等方法在非空物件引用上實現等價關係:

  • 自反性:對於任何非空引用值x,x.equals(x)返回true;

  • 對稱性:對於任何非空引用值x和y,當且僅當y.equals(x)返回true的時候,x.equals(y)返回true;

  • 傳遞性:對於任何非空引用值x、y和z,如果x.equals(y)返回true,y.equals(z)返回true,那麼x.equals(z)也返回true;

  • 一致性:對於任何非空引用值x和y,如果這兩個物件都沒有被修改,那麼多次呼叫x.equals(y)會始終返回true或false;

  • 對於任何非空引用值,x.equals(null)的返回值是false。

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

equals()方法是Object類中實現了最有辨別能力的最合理的等價關係。也就是說,對於任何非空引用值x和y,當x和y指向同一個物件的時候(x == y返回true),該方法返回true。

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

請注意為了保持hashCode方法的一致性,只要重寫equals方法的時候,就必須要重寫hanshCode方法,即相等的物件必須具有相等的雜湊值。

Parameters(引數項) Introduce (引數說明)
obj Object: the reference object with which to compare.(物件:用來進行比較的物件)
Returns(返回值) Introduce (返回值說明)
boolean true if this object is the same as the obj argument; false otherwise.(如果這個物件和傳入的物件是相等的,返回true否則返回false)