1、在java.lang包下,使用时不需要导包(使用lang包下的类的都不需要导包)
2、他是所有引用类型(包括我们自己定义的类,以及数组)的父类。所有类都隐式的继承Object,所以都是Object类的子类,意味着我们自己定义的类即使内部什么都没定义,也会拥有一定的成员
3、Object类是类层次结构中的根类
4、Object类的成员:查看帮助文档
5、相同的对象返回的hashCode值是相同的,不同的对象返回的值不同
6、"=="和equals()方法
6/1 ==符号:
6/1/1 作用在数值类型的操作数上,判断值是否相等
int a = 10;
int b = 10;
System.out.printlnl(a == b);
6/1/2 作用在引用各类型的操作数上,判断地址(引用)是否相等
Student sut1 = new Student();
Student stu2 = new Student();
System.out.println(stu1 == stu2);
6/2 equals()方法
6/2/1 Object的默认实现:只有当两个对象的地址相同时,才会返回true,所以需要重写equals()方法,使之只要对象的值相同就返回true
重写equals()方法:
class Student{
String name;
int age;
}
public boolean equals(Object obj){
//判断obj是不是某个类的对象,如果不是,就没有可比性,返回false
if(!(objinstanceof Student)){
return false;
}
//因为要访问Student的特有内容,所以要向下转型
Student stu = (Student)obj;
//判断是否相等
boolean b1 =(this.name.equals(stu.name));//String类也重写了equals()方法,用于判断两个字符串存储的字符序列是否完全相同
boolean b2 = (this.age == stu.age);
return b1 && b2;
}
7、某各类要调用Object类的clone()方法,这个类需要实现Cloneable接口
8、Object类常用方法
public String toString();
public int hashCode()
public boolean equals()
protected void finalize()
public final Class<?> getClass()
public final void wait()
public final void wait(long timeout)
public final void wait(long timeout, int nanos)
public final void notify()
public final void notifyAll()
1278

被折叠的 条评论
为什么被折叠?



