所有类的基类——Object

来源:互联网 发布:不基于比较的排序算法 编辑:程序博客网 时间:2024/05/16 17:23

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)
0 0
原创粉丝点击