Object类是Java所有类的基类,是整个类继承结构的顶端,也是最抽象的一个类。
Object中含有:registerNatives()、getClass()、hasCode()、equals()、clone()、toString()、notify()、notifyAll()、wait(long)、wait(long,int)、wait()、finalize()十二种方法。
registerNatives()
getClass()
public final natove Class getClass();
public方法可以通过对象直接调用,getClass方法就是获取当前类的对象在运行时类的所有信息的集合。
hashCode()
public native int hashCode();
public方法子类可以重写它,hashCode方法默认返回当前对象地址的hashCode值,重写过后就是自定义的计算方式,该值为整数范围内的(-2^31~2^31-1)。
import java.util.stream.Stream;
public class lengthannotation {
private int age;
private String name;
//注意构造器的创建与使用;
lengthannotation(int age,String name){
this.age=age;
this.name=name;
}
public int hashCode () {
//Stream.of()给指定元素创建顺序流;
//Stream.toArray()方法返回Object[]数组;
Object[] a = Stream.of(age, name).toArray();
int result = 1;
for (Object element : a) {
result = 31 * result + (element == null ? 0 : element.hashCode());
}
return result;
}
public static void main(String[] args) {
//创建对象之后再调用hashCode();
lengthannotation length=new lengthannotation(1,"赵赵");
System.out.println(length.hashCode());
}
}
equals()
public boolean equals(Object obj);
用于比较当前对象与目标对象是否相等,默认是比较引用(对象地址)是否指向同一对象,子类可以重写,在类内重写之后会调用重写后的equals方法。
重写的原因:在定义一个不允许有重复值的对象数组时,equals方法判断的是对象地址(引用),无论值一不一样,new出来的对象的内存地址肯定不同,所以equals方法无法判断,需要重写。
注意:在重写equals方法时一般也要重写hashCode方法。
没有重写equals()时:
public class Equalstest {
public static void main(String[] args) {
//创建List列表students盛放类型为Students的元素
List<Student> students = new ArrayList<>();
//声明Student类型的student对象,注意是声明,并没有创建;
Student student;
for(int i=0;i<10;i++){
//创建student对象,每迭代一次就会创建一个对象;
student=new Student("小明");
//判断students列表中是否包含该student,如果不包含,加入到列表中
//contains()方法,判断礼盒装是否含有某元素,有则返回true,没有则返回false;
//注意:contains()源码中使用了equals()方法
if(!students.contains(student)){
students.add(student);
}
}
System.out.println(students.size());//输出10
}
}
class Student{
private String name;
public Student(String name){
this.name=name;
}
}
重写了equals()时:
public class Equalstest2 {
public static void main(String[] args) {
List<Student2> students=new ArrayList<>();
Student2 student;
for(int i=0;i<10;i++){
student=new Student2("小明");
if(!students.contains(student)){
students.add(student);
}
}
System.out.println(students.size());//1
}
}
class Student2{
private String name;
public Student2(String name){
this.name=name;
}
public String getName(){
return name;
}
//重写equals方法
public boolean equals(Object obj){
if(obj==this){
return true;
}
if(obj instanceof Student2){
Student2 student = (Student2) obj;
if(student.getName().equals(this.name)){
return true;
}
}
return false;
}
}
clone()
protected native Object clone() throws CloneNotSupportedException;
protected方法,提供给子类重写,但需要实现Cloneable方法,这是一个标记接口,如果没有实现,当调用object.clone()方法,会抛出CloneNotSupportedException。
注意:使用clone()方法时必须要重写clone()方法。
toString()
public String toString();
public方法,子类可以重写,默认的toString方法返回的是当前类的全限定类名+@+十六进制的hashCode值。
如何重写:按照自己想要返回的内容来写。
public String toString() {
return "[name = " + getName() + ",age = "+ getAge() + "]";
}
notify()
notifyAll()
wait(long)
wait(long,int)
wait()
这三个方法是用来线程间通信用的,作用是阻塞当前线程,等待其他线程调用notify()/notifyAll()方法将其唤醒。这三个方法都是public final的,不可被重写。
finalize()