Object类是所有类的始祖,但是不用显示的用extends关键字,如果没有extends任何类,Object就会被认为是这个类的超类。
在Java中,只有基本类型(byte、short、char、int、long、float、double、boolean)不是对象。但是基本类型数组都是拓展于Object类。
Object类中常用的方法:
public boolean equals:用于检测一个对象是否等于另外一个对象,在Object类中,这个方法将判断两个对象是否具有相同的引用。如果两个对象具有相同的引用。
instanceof:是Java的一个二元操作符,主要用于判断子类是否为父类的实现
在Java中包含了150多个equals方法的实现,包括使用instanceof、getClass、捕获ClassCastException等等;
public native int hashCode方法:
hashCode(散列码)native方法,是由对象导出的一个整型值。散列码是由对象hash算法得来的。如果x和y是两个不同的对象,x.hashCode()基本上不等于y.hashCode()。hashCode的值为对象的存储地址;
public String toString方法:
用于返回表示对象值的字符串,一般在建entity类时可以重写此方法,因为直接调用object类的toString方法获得的字符串是:类名+@+哈希编码;数组也需要重写toString方法:类型缩写+@+哈希编码;
public final native Class<?> getClass方法:final native方法,返回包含对象信息的类对象。包路径加类名;
private static native void registerNatives();这需要学习JNI(JavaNative Interface);
static {
registerNatives();
}
protected native Object clone() throws CloneNotSupportExecption;
使用这个方法需要实现Cloneable接口,重写子类clone方法调用父类clone方法
package fuxi;
import java.util.Date;
public class Employee implements Cloneable {
private String name;
private double salary;
private Date hireDay;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Date getHireDay() {
return hireDay;
}
public void setHireDay(Date hireDay) {
this.hireDay = hireDay;
}
@Override
public String toString() {
return "Employee [name=" + name + ", salary=" + salary + ", hireDay=" + hireDay + "]";
}
public Employee(String name, double salary, Date hireDay) {
this.name = name;
this.salary = salary;
this.hireDay = hireDay;
}
public Employee() {
super();
}
/**默认的是protected访问,需要改成public,在这个方法中没有克隆所有可变域,这是浅拷贝*/
@Override
public Employee clone() throws CloneNotSupportedException {
return (Employee)super.clone();
}
/**深拷贝,可变域都要拷贝*/
@Override
public Employee clone() throws CloneNotSupportedException {
Employee cloned = (Employee) super.clone();
cloned.hireDay = (Date) hireDay.clone();
return cloned;
}
}
以下是多线程相关的知识:
public final native void notify():
public final native void notifyAll():
public final native void wait() throws InterruptedException:
public final native void wait(long timeout) throws InterruptedException:
public final native void wait(long timeout,int nanos) throws InterruptedException:
protected void finalize() throws Throwable{}