浅拷贝:

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. 1.2 JAVA的String类和StringBuffer类

    一.String 1.String概念 String不属于基本类型,String是final修饰的是不可改变的,所以你一旦创建了 String 对象,那它的值就无法改变了如果要对String修改使用 ...

  2. Error creating bean with name 'persistenceExceptionTranslationPostProcessor' defined in class path resource [org/springframework/boot/autoconfigure/dao/PersistenceExceptionTranslationAutoConfiguration

    dubbo 包和SpringBoot 冲突,注释就可以正常启动

  3. openfalcon架构及相关服务配置详解(转)

    一:openfalcon组件 1.falcon-agent 数据采集组件 agent内置了一个http接口,会自动采集预先定义的各种采集项,每隔60秒,push到transfer. 2.transfe ...

  4. shell脚本编程数组

    数组: 变量:存储单个元素的内存空间 数组:存储多个元素的连续的内存空间,相当于多个变量的集合 数组名和索引 索引:编号从0开始,属于数值索引 注意:索引可支持使用自定义的格式,而不仅是数值格式,即为 ...

  5. Ubuntu 配置ISCSI服务

    摘要:sudo apt-get install iscsitarget立刻搞定, 然后编辑配置文件:sudovim/etc/ietd.conf默认的配置文件, 有详细的配置说明和示例,本博先备份了事, ...

  6. Qt网络获取本机网络信息

    下面我们就讲解如何获取自己电脑的IP地址以及其他网络信息.这一节中,我们会涉及到网络模块(QtNetwork Module)中的QHostInfo ,QHostAddress ,QNetworkInt ...

  7. How to use reminder feature of the outlook

    https://support.office.com/en-us/article/set-or-remove-reminders-7a992377-ca93-4ddd-a711-851ef359792 ...

  8. Reflexil

    https://github.com/sailro/Reflexil/issues/47 Instructions on how to install Reflexil would be much a ...

  9. webpack publicpath path

    一.publicpath 用绝对路径:如 /assets/ 会在支援路径前 加上 /assets/ devServer  publicpath 如果没有设置的话,取 publicpath 所以 一般要 ...

  10. ps在psd格式图片里面切图流程

    第一. 第二. xx的地方自己重新命名 第三. 第四.