ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

10_Java基类Object

2022-05-16 00:00:43  阅读:197  来源: 互联网

标签:10 Java human clone Object Human mother public name


java.lang.Object 是 Java 类结构中其他所有类的超类。

clone

protected native Object clone() throws CloneNotSupportedException;

clone 方法返回当前对象的副本对象。

Object 将 clone 作为一个本地方法来实现。当执行 clone 的时候,会检查调用对象的类(或者父类)是否实现了java.lang.Cloneable接口( Object 类不实现 Cloneable )。如果没有实现这个接口,将会抛出一个检查异常 — java.lang.CloneNotSupportedException,如果实现了这个接口,会创建一个新的对象,并将原来对象的内容复制到新对象,最后返回这个新对象的引用。

浅克隆与深克隆

Object 类的 clone 方法是浅克隆,浅克隆对于字符串以外的引用数据类型克隆的是地址。深克隆则可以进行完全克隆。

  • 浅克隆
public class Human implements Cloneable {

    public String name;

    public int age;

    public Human mother;

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public static void main(String[] args) throws CloneNotSupportedException {
  Human mother = new Human();
  mother.name = "gm";
  mother.age = 50;

  Human human = new Human();
  human.name = "kh";
  human.age = 25;
  human.mother = mother;

  Human copyMan = (Human) human.clone();

  System.out.println(human == copyMan); // false
  System.out.println(human.name == copyMan.name); // true
  System.out.println(human.mother == copyMan.mother); // true

}
  • 深克隆
public class Human implements Cloneable {

    public String name;

    public int age;

    public Human mother;

    @Override
    public Object clone() throws CloneNotSupportedException {
        Human human = (Human) super.clone();
        if(mother != null) {
            human.mother = (Human) mother.clone();
        }
        return human;
    }
}
public static void main(String[] args) throws CloneNotSupportedException {
  Human mother = new Human();
  mother.name = "gm";
  mother.age = 50;

  Human human = new Human();
  human.name = "kh";
  human.age = 25;
  human.mother = mother;

  Human copyMan = (Human) human.clone();

  System.out.println(human == copyMan); // false
  System.out.println(human.name == copyMan.name); // true
  System.out.println(human.mother == copyMan.mother); // false

}

toString

public String toString() {
        return this.getClass().getName() + "@" + Integer.toHexString(this.hashCode());
}

源码中 toString 方法返回的是 类名 + "@" + 当前对象的哈希值,以此表示当前对象,可根据需要重写。

equals 和 hashCode

public boolean equals(Object var1) {
  return this == var1;
}
public native int hashCode();
  • 如果根据 equals 方法两个对象相等,则 hashCode 相等;equals 不相等,hashCode 可能相等

hashCode 相等,equals 可能不相等,但是,为 equals 不等对象生成不同的 hashCode 可能会提高哈希表的性能。

  • 一般需要同时重写 equals 和 hashCode 方法,确保 equals 相等时 hashCode 相等。

finalize

protected void finalize() throws Throwable {
}

当垃圾收集确定不再有对该对象的引用时,垃圾收集器在对象上调用。

getClass

public final native Class<?> getClass();

返回对象的运行时类。

标签:10,Java,human,clone,Object,Human,mother,public,name
来源: https://www.cnblogs.com/knhap/p/16275320.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有