常用类之Object

1.hashCode的理解

public class TestObject1 {

    public static void main(String[] args) {
        //hashCode是根据对象的内部地址再转换的一个整数
        Student s1 = new Student();
        System.out.println(s1.hashCode());
        
        Student s2 = new Student();
        System.out.println(s2.hashCode());
        
        //由于hashCode是根据对象的内部地址转换的,把s1的引用付给了s3,s1跟s3地址一样,hashCode也一样
        Student s3 = s1;
        System.out.println(s3.hashCode());
        
    }
}
353591321
722080798
353591321

2.getClass.返回是运行时的类

public class TestObject1 {

    public static void main(String[] args) {
        Student s = new Student();
        //返回运行时类
        Class c = s.getClass();
        System.out.println(c.getName());
        
    }
}

3.toString

public class TestObject1 {

    public static void main(String[] args) {
        Human human = new Human();
        
        System.out.println(human.hashCode());
        System.out.println(human.getClass().getName());
        //将对象转换为字符串。如果不重写,默认是getClass().getName()+@+Integer.toHexString(hashCode());
        System.out.println(human.getClass().getName()+"@"+Integer.toHexString(human.hashCode()));
        System.out.println(human.toString());
        
        //直接输出对象。也就是输出human.toString()方法
        System.out.println(human);
    }
}

Integer.toHexString(int i )将一个十进制的整数转成16进制的数

258702862
cn.eminem_07.Human
cn.eminem_07.Human@f6b7e0e
cn.eminem_07.Human@f6b7e0e
cn.eminem_07.Human@f6b7e0e

4.equals

  不重写equals的情况下:

public class TestObject1 {

    public static void main(String[] args) {

       Human h1 = new Human(1,"zs");
      Human h2 = new Human(1,"zs");

        Human h3 = h1;
        // 比较的是引用类型
        System.out.println(h1 == h2);
        // equals如果不重写,默认比较的是对象的引用地址
        System.out.println(h1.equals(h2));

        System.out.println(h1.equals(h3));
    }
}

结果:

false
false
true

重写equal方法的话:

public class Human {

    private int age;
    private String name;

    public Human() {
        super();
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Human(int age, String name) {
        super();
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Human [age=" + age + ", name=" + name + "]";
    }

    @Override
    public boolean equals(Object obj) {
        Human s = (Human) obj;
        //equal 默认是比较对象,这边重写比较对象的属性值
        if(this.name.equals(s.name) && this.age == s.age){
            return true;
        }else{
            return false;
        }
    }
    
}

 重写后,结果:

false
false
true

优化重写的内容:

package cn.eminem_08;

public class Human {

    private int age;
    private String name;

    public Human() {
        super();
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Human(int age, String name) {
        super();
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Human [age=" + age + ", name=" + name + "]";
    }

    @Override
    public boolean equals(Object obj) {
        
        //同一对象没必要比较
        if(this == obj){
            return true;
        }
        
        //判断obj是不是Human类,不是Human类,没必要向下转型再比较、
        //使用instanceof 判断对象是否是某个类
        if(!(obj instanceof Human)){
            return false;
        }
        
        Human s = (Human) obj;
        System.out.println("同一对象,还需要向下转型进行比较吗?");
        return this.name.equals(s.name) && this.age == s.age;
    }
    
}

 测试结果:

public class TestObject1 {

    public static void main(String[] args) {

        Human h1 = new Human(1,"zs");
        Human h2 = new Human(1,"zs");
        Human h3 = h1;
        Human h4 = new Human(1,"ls");
        
        // 比较的是引用类型
        System.out.println(h1 == h2);
        
        // 不是同一个对象 equals如果不重写,默认比较的是对象的引用地址
        System.out.println(h1.equals(h2));

        //是同一对象
        System.out.println(h1.equals(h3));

        //不是同一个对象
        System.out.println(h1.equals(h4));
    }
}
false
同一对象,还需要向下转型进行比较吗?
true
true
同一对象,还需要向下转型进行比较吗?
false

 5.finalize

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

 

6.clone

public class TestObject1 {

    public static void main(String[] args) throws CloneNotSupportedException {

        Human h = new Human();
        //克隆新对象obj
        Object obj = h.clone();
        
        Human h2 = (Human) obj;
    }
}

结果:

出现异常CloneNotSupportedException,所以要克隆的类要实现标记接口Cloneable

此类实现了 Cloneable 接口,以指示 Object.clone() 方法可以合法地对该类实例进行按字段复制

package cn.eminem_09;

public class Human implements Cloneable{

    private int age;
    private String name;

    public Human() {
        super();
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Human(int age, String name) {
        super();
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Human [age=" + age + ", name=" + name + "]";
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

clone方法的目的

public class TestObject1 {

    public static void main(String[] args) throws CloneNotSupportedException {

        Human h = new Human(1, "zs");
        // 克隆新对象obj
        Object obj = h.clone();

        Human h2 = (Human) obj;

        Human h3 = h;
        
        System.out.println(h.getName());
        System.out.println(h2.getName());
        System.out.println(h3.getName());

        System.out.println("-----------");
        
        h3.setName("ls");
        //
        System.out.println(h.getName());
        System.out.println(h2.getName());
        //由于h3跟h1引用同一对象,h3.name改变,h1.name也改变。但是克隆的对象不变
        //也就是说clone出来的是新对象。地址值不一样
        System.out.println(h3.getName());
        
    }

 

posted @ 2016-05-09 17:20  倔强的鸭子  阅读(197)  评论(0编辑  收藏  举报