Java Object类的常用方法

2019-02-19  本文已影响0人  夜阑w

一、clone

1、clone()方法

在实际编程过程中,常常遇到这种情况:有一个对象A,在某一时刻A中已经包含了一些有效值,此时可能会需要一个和A完全相同新对象B,并且此后对B任何改动都不会影响到A中的值,也就是说,A与B是两个独立的对象,但B的初始值是由A对象确定的。在Java语言中,用简单的赋值语句是不能满足这种需求的。实现clone()方法是满足需求最简单,也是最高效的手段。

clone()将返回Object对象的一个拷贝。要说明的有两点:一是拷贝对象返回的是一个新对象,而不是一个引用。二是拷贝对象与用 new操作符返回的新对象的区别就是这个拷贝已经包含了一些原来对象的信息,而不是对象的初始信息。

2、例子
public class Test implements Cloneable{
    private double value;

    public Test(double value) {
        this.value = value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    public double getValue() {
        return this.value;
    }

    public Object clone() {
        Test copy = null;
        try {
            copy = (Test) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return copy;
    }
}

public class Main {
    public static void main(String[] args) {
        Test dh = new Test(100.00);
        Test dhClone = (Test) dh.clone();

        System.out.println("Original:" + dh.getValue()); //Original:100.0
        System.out.println("Clone :" + dhClone.getValue()); //Clone :100.0

        dh.setValue(200.00);
        dhClone.setValue(400.00);

        System.out.println("Original:" + dh.getValue()); //Original:200.0
        System.out.println("Clone :" + dhClone.getValue()); //Clone :400.0
    }
}
3、Shallow Cloning
public class Test {
    private double value;

    public Test(double value) {
        this.value = value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    public double getValue() {
        return this.value;
    }

public class ShallowClone implements Cloneable {
    public Test holder = new Test(0.0);
    public Object clone() {
        ShallowClone copy = null;
        try {
            copy = (ShallowClone) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return copy;
    }
}

public class Main {
    public static void main(String[] args) {
        ShallowClone  dh = new ShallowClone();
        dh.holder.setValue(100.00);
        ShallowClone  dhClone = (ShallowClone ) dh.clone();

        System.out.println("Original:" + dh.holder.getValue()); //Original:100.0
        System.out.println("Clone :" + dhClone.holder.getValue()); //Clone :100.0

        dh.holder.setValue(200.00);

        System.out.println("Original:" + dh.holder.getValue()); //Original:200.0
        System.out.println("Clone :" + dhClone.holder.getValue()); //Clone :200.0
    }
}

此例子中当ShallowClone类使用super.clone()调用Object类的clone()方法时,它会接收自身的浅拷贝。也就是说,它与其克隆共享其实例变量中使用的holder对象。

4、Deep Cloning

在深层克隆中,需要克隆对象的所有引用实例变量引用的所有对象。

public class Test implements Cloneable{
    private double value;

    public Test(double value) {
        this.value = value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    public double getValue() {
        return this.value;
    }

    public Object clone() {
        Test copy = null;
        try {
            copy = (Test) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return copy;
    }
}

public class DeepClone implements Cloneable {
    public Test holder = new Test(0.0);
    public Object clone() {
        DeepClone copy = null;
        try {
            copy = (DeepClone) super.clone();
            copy.holder = (Test) this.holder.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return copy;
    }
}

public class Main {
    public static void main(String[] args) {
        DeepClone dh = new DeepClone();
        dh.holder.setValue(100.00);
        DeepClone dhClone = (DeepClone) dh.clone();

        System.out.println("Original:" + dh.holder.getValue()); //Original:100.0
        System.out.println("Clone :" + dhClone.holder.getValue()); //Clone :100.0

        dh.holder.setValue(200.00);

        System.out.println("Original:" + dh.holder.getValue());  //Original:200.0
        System.out.println("Clone :" + dhClone.holder.getValue()); //Clone :100.0
    }
}

二、equals

1、概述

equals 方法默认比较的是两个对象的引用是否指向同一个内存地址。hashCode 是一个 native 本地方法,默认的 hashCode 方法返回的就是对象对应的内存地址。

equals 方法是基类 Object 的方法,我们创建的所有的对象都拥有这个方法,并有权利去重写这个方法。该方法返回一个 boolean 值,代表比较的两个对象是否相同,这里的相同的条件由重写 equals 方法的类来解决。

String str1 = "abc";
String str2 = "abc";
str1.equals(str2);//true

String 类重写了 equals 方法,否则两个 String 对象内存地址肯定不同。

2、equals()方法的实现的规范

假设x,y和z是三个对象的非空引用。

3、equals 与 == 的区别

public class Test {
    public String name="abc";
    public static void main(String[] args) {
        Test test=new Test();
        Test testb=new Test();
        String a=new String("a");
        String b=new String("a");
        System.out.println(test.equals(testb));//false
        System.out.println(test==testb);//false
        System.out.println(test.name.equals(testb.name));//true
        System.out.println(a==b);//false
        System.out.println(a.equals(b));//true
    }   
}

4、equals()和hashCode()的联系

三、getClass

Object类中包含一个方法名叫getClass,利用这个方法就可以获得一个实例的类型类。类型类指的是代表一个类型的类,因为一切皆是对象,类型也不例外,在Java使用类型类来表示一个类型。所有的类型类都是Class类的实例。

一般情况下,getclass()方法和class方法是等价的,都可以获得一个类型名,例:

class A{
    public void func(){

    }
}

public class Test {
    public static void main(String[] args) {
        A a = new A();
        System.out.println(a.getClass()+" "+A.class); //class A class A
    }
}

getclass()和class的区别

四、toString

toString()是Object类的一个公有方法,而所有类都继承自Object类。所以所有类即使不实现toString方法,也会存在从Object类继承来的toString。Object类提供了toString()方法的默认实现。它返回一个以下格式的字符串:

<fully qualified class name>@<hash code of object in hexadecimal>

一个例子

import java.util.ArrayList;

public class ObjectDemo {
   public static void main(String[] args) {

      Integer i = new Integer(50);

      ArrayList list = new ArrayList();

      list.add(50);
      list.add("Hello World");

      System.out.println("" + i.toString());  //50
      System.out.println("" + list.toString()); //[50, Hello World]
   }
}

类可以实现toString方法,在控制台中打印一个对象会自动调用对象类的toString方法,所以我们可以实现自己的toString方法在控制台中显示关于类的有用信息。

public class Main{
  public static void main(String[] argv){
    MyClass obj  = new MyClass(123);
    String objStr = obj.toString();
    System.out.println(objStr);
  }
}
class MyClass {
  private int value;

  public MyClass(int value) {
    this.value = value;
  }

  public void setValue(int value) {
    this.value = value;
  }

  public int getValue() {
    return value;
  }

  public String toString() {
    String str = String.valueOf(this.value);
    return str;
  }
}
上一篇 下一篇

猜你喜欢

热点阅读