正文
有的童鞋会问,Java类不是单继承么?如果一个类继承了其他父类,就不会直接继承Object,但是它的父类如果再没有父类,就会继承Object,也就是说,Object是任何一个类的直接或间接的父类。因此,我们随意创建一个类,其实也不是空的,起码这个类中有Object类中的属性和方法。
例:
public class Example {
public Example() {
}
}
public class Client {
public static void main(String[] args) {
//任何类都是Object的派生类或者子类
Example example = new Example();
System.out.println(example.equals(null));
System.out.println(example.getClass());
System.out.println(example.hashCode());
System.out.println(example.toString());
}
}
输出:
false
class com.company.project.demo0.Example
2018699554
com.company.project.demo0.Example@7852e922
1、Object类的作用和地位
Object类在java.lang包下,是类层次结构的根类。每个类都使用 Object 作为超类。所有对象(包括数组)都实现了这个类的方法。
例:
public class Example {
public static void main(String[] args) {
//数组也是Object的子类
int[] array = new int[0];
System.out.println(array.equals(null));
System.out.println(array.getClass());
System.out.println(array.hashCode());
System.out.println(array.toString());
}
}
输出:
false
class [I
2018699554
[I@7852e922
2、Object类的方法
protected Object clone()
Creates and returns a copy of this object.
创建并返回此对象的一个副本。
boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.
判断其他某个对象是否与此对象“相等”。
protected void finalize()
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.
当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。 一个子类覆盖finalize方法来处理系统资源或执行其他清理。
Class<?> getClass()
Returns the runtime class of this Object.
返回此 Object 的运行时类。
int hashCode()
Returns a hash code value for the object.
返回该对象的哈希码值。
void notify()
Wakes up a single thread that is waiting on this object's monitor.
唤醒在此对象监视器上等待的单个线程。
void notifyAll()
Wakes up all threads that are waiting on this object's monitor.
唤醒在此对象监视器上等待的所有线程。
String toString()
Returns a string representation of the object.
返回该对象的字符串表示。
void wait()
Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.In other words, this method behaves exactly as if it simply performs the call wait(0).
导致当前线程等待,直到另一个线程调用此对象的notify()方法或notifyAll()方法。换句话说,这个方法的行为与它简单地执行调用wait(0)完全一样。
void wait(long timeout)
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()方法,或者经过指定的时间量。
void wait(long timeout, 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()方法,或其他一些线程中断当前线程,或已经过了一定时间。
3、常用方法
由于Object类被所有类直接或间接继承,所以任何引用类型中都有Object类中的方法。学习Object类中的常用方法,便于理解其他引用类型类。
3.1、toString方法
toString方法可以返回该对象的字符串表示。或者说将任何一个对象转换成字符串返回。方法定义如下:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
通常,toString 方法会返回一个“以文本方式表示”此对象的字符串。结果应是一个简明但易于读懂的信息表达式。建议所有子类都重写此方法。
Object 类的 toString 方法返回一个字符串,该字符串由类名(对象是该类的一个实例)、at 标记符“@”和此对象哈希码的无符号十六进制表示组成。换句话说,该方法返回一个字符串,它的值等于:
getClass().getName() + '@' + Integer.toHexString(hashCode())
例:
输出:
3.2、equals方法和==
3.2.1、equals方法
equals方法判断其它某个对象是否与此对象“相等”。方法定义如下:
public boolean equals(Object obj) {
return (this == obj);
}
由方法定义可知,Object类中的equals方法,用来比较两个引用的虚地址。当且仅当两个引用在物理上是同一个对象时,返回值为true,否则将返回false。
equals 方法在非空对象引用上实现相等关系:
●自反性:对于任何非空引用值 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,前提是对象上 equals 比较中所用的信息没有被修改。
●对于任何非空引用值 x,x.equals(null) 都应返回 false。
Object 类的 equals 方法实现对象上差别可能性最大的相等关系;即,对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象时,此方法才返回 true(x == y 具有值 true)。
注意:
当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。
例:
输出:
3.2.2、==
==比较的是内存中的虚地址。
例:
输出: