Object类

来源:互联网 发布:陕西省科协网络平台 编辑:程序博客网 时间:2024/06/06 03:32

Object类

1.类层次结构的根类。每个类都使用Object作为超类。

  • 所有类都直接或间接的继承自该类。

2.Object类的构造方法有一个,并且是无参构造。

  • 这其实就是解释当时我们说过,子类的构造方法默认访问的是父类的无参构造方法。

3.要掌握的方法:

public String toString();

  • 返回对象的字符串表示,默认是由类的全路径+’@’+哈希值的十六进制表示。
public class StudentDemo {    public static void main(String[] args) {        Student s = new Student();        System.out.println(s.hashCode());        System.out.println(s.getClass().getName());        System.out.println("----------");        System.out.println(s.toString());        System.out.println("----------");        //toString()原理        System.out.println(s.getClass().getName() + '@'                + Integer.toHexString(s.hashCode()));    }}
  • 这个表示其实是没有意义的,一般子类都会重写该方法。
    如何重写呢?过程我也讲解过了,基本上就是要求信息简单明了。
  • 但是最终还是自动生成。
  • 直接输出一个对象的名称,其实就是调用该对象的toString()。

public boolean equals(Object obj);

  • 默认比较的是地址值。一般来说意义不大,所以我们要重写
    怎么重写呢?
    一般都是用来比较对象的成员变量是否相同。
    重写的代码优化:提高效率,提高程序的健壮性。
  • 最终版:自动生成。
@Overridepublic boolean equals(Object obj) {    if (this == obj)        return true;    if (obj == null)        return false;    if (getClass() != obj.getClass())        return false;    Student other = (Student) obj;    if (age != other.age)        return false;    if (name == null) {        if (other.name != null)            return false;    } else if (!name.equals(other.name))        return false;    return true;}   /*  public boolean equals(Object obj) { //      return super.equals(obj);    //我们要使用的是学生类的特有成员变量,所以要向下转型    //为了提高效率    if(this == obj) {        return true;    }       //为了提高程序的健壮性。    //我先判断obj是不是学生的一个对象,如果是再做向下转型,如果不是,直接返回false.    //这时,我们要判断对象是不是某个类的对象?    //格式: 对象名 instanceof 类名    //表示:判断该对象名是否是该类名一个对象    if(!(obj instanceof Student)) {        return false;    }    Student s = (Student)obj;    System.out.println("同一对象,还用比较?");//      if(this.name.equals(s.name) && this.age == s.age) {//          return true;//      } else {//          return false;//      }    return this.name.equals(s.name) && this.age == s.age;}*/ 
  • 面试题:

    • ==:
      基本类型:值是否相同。
      引用类型:地址值是否相同
    • equals:
      引用类型:默认情况下, 比较的是地址值是否相同。
      不过,我们可以根据情况自己重写。一般重写都是自动生成,比较对象的成员变量值是否相同。
  • 源码:
public boolean equals(Object obj) {    return (this == obj);}   

4.了解的方法:

public int hashCode();

  • 返回该对象的哈希码值。(哈希值根据哈希算法计算出来的一个值,这个值和地址值有关,但不是实际地址值)

public final Class getClass();

  • 返回此Object的运行时类。
    Class类的方法:
    public String getName():以String的形式返回此Class对象所表示实现对象。
public class Test {    public static void main(String[] args) {//      Student02 s = new Student02();//      System.out.println(s.hashCode());//      //      //      Student02 s2 = new Student02();//      System.out.println(s2.hashCode());//              Student02 s = new Student02();        Class c = s.getClass();        String str = c.getName();        System.out.println(str);//com.object_02.Student02        //链式编程。        String str2 = s.getClass().getName();        System.out.println(str2);    }}   

protected void finalize();

  • 当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。用于垃圾回收,但是什么时候回收不确定。

protected Object clone();

  • 创建并返回此对象的一个副本。
    A:重写该方法
  • Cloneable:此类实现了Cloneable接口,以指示Object.clone()方法可以合法地对该类实例进行按字段复制。
    这个接口是标记接口,告诉我们实现该接口的类就可以实现对象的复制了。
public class StudentDemo02 {    public static void main(String[] args) throws CloneNotSupportedException {        Student s1 = new Student("qi", 27);//      Student s2 = new Student("qi", 27);//      System.out.println(s1==s2);//      //      Student s3 = s1;//      System.out.println(s1==s3);//      System.out.println("-----------");//      //      System.out.println(s1.equals(s2));//      System.out.println(s1.equals(s1));//      System.out.println(s1.equals(s3));//      Demo d = new Demo();//      System.out.println(s1.equals(d));        Student s = new Student("qiwei", 22);        //克隆        Object obj = s.clone();        Student s2 = (Student)obj;        System.out.println(s.getName() + "--" + s.getAge());        System.out.println(s2.getName() + "--" + s2.getAge());        //以前的做法        Student s3 = s;        System.out.println(s3.getName() + "--" + s3.getAge());    }}
0 0
原创粉丝点击