浅拷贝:

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. jQuery系列(九):JS的事件流的概念

    1.事件概念 HTML中与javascript交互是通过事件驱动来实现的,例如鼠标点击事件.页面的滚动事件onscroll等等,可以向文档或者文档中的元素添加事件侦听器来预订事件.想要知道这些事件是在 ...

  2. maven创建可执行jar包项目的配置

    <build> <plugins> <plugin> <artifactId>maven-shade-plugin</artifactId> ...

  3. MyBatis动态Sql 的使用

    Mapper.xml提示: 1:mapper包中新建一个文件:mybatis-3-mapper.dtd 2:在web app libraries/mybatis.jar/org.apache.ibat ...

  4. Android RecyclerView实现加载多种条目类型

    今天咱们是用RecyclerView来实现这个多种Item的加载. 其实最关键的是要复写RecyclerView的Adapter中的getItemViewType()方法 这个方法就根据条件返回条目的 ...

  5. springboot 获取控制器参数的几种方式

    这里介绍springboot 获取控制器参数有四种方式 1.无注解下获取参数 2.使用@RequestParam获取参数 3.传递数组 4.通过URL传递参数 无注解下获取参数无注解下获取参数,需要控 ...

  6. [Python]统计1个元素在列表中的出现次数

    使用列表自带的count方法: list.count(element) 示例:  列表a,有4个元素,其中值1出现3次 In []: a=[,,,] In []: a Out[]: [, , , ] ...

  7. php缓存加速优化--Xcache

    1.安装软件:cd /usr/local/src/下载软件包wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache- 3.2.0.tar.b ...

  8. Lucence简单学习---1

    package cn.itheima.lucene; import java.io.File; import java.util.ArrayList; import java.util.List; i ...

  9. [Spark] Scala programming - basic level

    环境配置 IDE: https://www.jetbrains.com/idea/ 子雨大数据之Spark入门教程(Scala版) /* implement */ 语言特性 Online compil ...

  10. JAVA与c#中byte取值范围的差异

    C#中分有符号类型的sbyte和无符号类型的byte Console.WriteLine("byte.min:{0},byte.max:{1},{2}byte", byte.Min ...