浅拷贝:

class Professor {
String name;
int age; public Professor(String name, int age) {
this.name = name;
this.age = age;
} @Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
} class Student implements Cloneable {
String name;
int age;
Professor p; public Student(String name, int age, Professor p) {
this.name = name;
this.age = age;
this.p = p;
} @Override
protected Object clone() throws CloneNotSupportedException {
Student s = (Student) super.clone();
// s.p = (Professor) p.clone();
return s;
}
} public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Professor p = new Professor("教授1", 50);
Student s1 = new Student("学生1", 18, p);
Student s2 = (Student) s1.clone(); s2.p.name = "教授2";
s2.p.age = 30;
s2.name = "学生2";
s2.age = 25;
System.out.println(s1.name + " " + s1.age + " " + s1.p.name + " " + s1.p.age);
}
}

结果: 学生1 18 教授2 30

深拷贝:

class Professor implements Cloneable {
String name;
int age; public Professor(String name, int age) {
this.name = name;
this.age = age;
} @Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
} class Student implements Cloneable {
String name;
int age;
Professor p; public Student(String name, int age, Professor p) {
this.name = name;
this.age = age;
this.p = p;
} @Override
protected Object clone() throws CloneNotSupportedException {
Student s = (Student) super.clone();
s.p = (Professor) p.clone();
return s;
}
} public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Professor p = new Professor("教授1", 50);
Student s1 = new Student("学生1", 18, p);
Student s2 = (Student) s1.clone(); s2.p.name = "教授2";
s2.p.age = 30;
s2.name = "学生2";
s2.age = 25;
System.out.println(s1.name + " " + s1.age + " " + s1.p.name + " " + s1.p.age);
}
}

结果: 学生1 18 教授1 50

序列化形式深拷贝:

class Professor implements Serializable {
private static final long serialVersionUID = 1286716519490813020L;
String name;
int age; public Professor(String name, int age) {
this.name = name;
this.age = age;
}
} class Student implements Serializable {
private static final long serialVersionUID = -547004870369127943L;
String name;
int age;
Professor p; public Student(String name, int age, Professor p) {
this.name = name;
this.age = age;
this.p = p;
} public Object deepClone() throws IOException, ClassNotFoundException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(this); ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
return objectInputStream.readObject();
}
} public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Professor p = new Professor("教授1", 50);
Student s1 = new Student("学生1", 18, p);
Student s2 = (Student) s1.deepClone(); s2.p.name = "教授2";
s2.p.age = 30;
s2.name = "学生2";
s2.age = 25;
System.out.println(s1.name + " " + s1.age + " " + s1.p.name + " " + s1.p.age);
}
}

结果: 学生1 18 教授1 50

深拷贝 & 浅拷贝的更多相关文章

  1. c# 内存的具体表现- 通用类型系统 深拷贝 浅拷贝 函数传参

    c# 通用类型系统 及变量在 深拷贝 浅拷贝 函数传参 中的深层次的表现 在编程中遇到了一些想不到的异常,跟踪发现,自己对于c#变量在内存上的表现理解有偏差,系统的学习并通过代码实验梳理了各种情况下, ...

  2. python集合增删改查,深拷贝浅拷贝

    集合 集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 去重,把一个列表变成集合,就自动去重了. 关系 ...

  3. JavaScript之深拷贝&浅拷贝

    深拷贝&浅拷贝,说起来都明白,但是说不出所以然.今天就系统的整理下思绪,一点点的将其分析出所以然 废话不多说 浅拷贝 简单的说就是一个值引用,学生时代接触过编程的人都应该了解过指针,浅拷贝可以 ...

  4. 【opencv】imread 赋值 深拷贝浅拷贝

    import cv2 import copy import os def filter_srcimg(dstimg): ss=3 srcimg=copy.deepcopy(dstimg) #aa=5 ...

  5. Java基础 深拷贝浅拷贝

    Java基础 深拷贝浅拷贝 非基本数据类型 需要new新空间 class Student implements Cloneable{ private int id; private String na ...

  6. 【04】Python 深拷贝浅拷贝 函数 递归 集合

    1 深拷贝浅拷贝 1.1 a==b与a is b的区别 a == b    比较两个对象的内容是否相等(可以是不同内存空间) a is b  比较a与b是否指向同一个内存地址,也就是a与b的id是否相 ...

  7. JS Object Deep Copy & 深拷贝 & 浅拷贝

    JS Object Deep Copy & 深拷贝 & 浅拷贝 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Refe ...

  8. Map拷贝 关于对象深拷贝 浅拷贝的问题

    问题:map拷贝时发现数据会变化. 高能预警,你看到的下面的栗子是不正确的,后面有正确的一种办法,如果需要看的话的,请看到底,感谢各同学的提醒,已做更正,一定要看到最后      先看例子:     ...

  9. clone 深拷贝 浅拷贝

    1. 定义:知道一个对象,但不知道类,想要得到该对象相同的一个副本,在修改该对象的属性时,副本属性不修改,clone的是对象的属性 2. 意义:当一个对象里很多属性,想要得到一个相同的对象,还有set ...

  10. Python深复制浅复制or深拷贝浅拷贝

    1. copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象.(比深拷贝更加节省内存)2. copy.deepcopy 深拷贝 拷贝对象及其子对象 用一个简单的例子说明如下: >& ...

随机推荐

  1. Ubuntu16.04从源码部署安装禅道过程记录

    1.首先把基础的lamp环境搭建好,这里利用apt安装即可 sudo apt install mysql-server sudo apt install apache2 sudo apt instal ...

  2. 使用 kubeadm 安装 kubernetes v1.16.0

    近日通过kubeadm 安装 kubernetes v1.16.0,踩过不少坑,现记录下安装过程. 安装环境: 系           统:CentOS Linux release 7.6 Docke ...

  3. 比较全的解释了:JAVA反射机制

    JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制 ...

  4. echart itemStyle属性设置

    itemStyle // itemStyle 设置饼状图扇形区域样式              itemStyle: {                // emphasis:英文意思是 强调;着重; ...

  5. 性能监控系统 | 从0到1 搭建Web性能监控系统

    工具介绍 1. Statsd 是一个使用Node开发网络守护进程,它的特点是通过UDP(性能好,及时挂了也不影响主服务)或者TCP来监听各种数据信息,然后发送聚合数据到后端服务进行处理.常见支持的「G ...

  6. buildscript和allprojects的作用和区别是什么?

    在Android Studio的Project的build.gradle中, // Top-level build file where you can add configuration optio ...

  7. Springboot整合 mybatis-generator

    1.pom.xml文件中 生成依赖 <plugin> <groupId>org.mybatis.generator</groupId> <artifactId ...

  8. 动态绑定v-model

    <template> <div class="pieAll" > <template v-for="(item, index) in tes ...

  9. easyUI之progressbar进度条

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  10. Mybati example generatorConfig.xml 配置详解

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...