当前位置:网站首页>面试:什么是浅拷贝、深拷贝?
面试:什么是浅拷贝、深拷贝?
2022-06-12 16:05:00 【眠秋雨】
浅拷贝也会在堆上创建一个对象,但是如果原对象中有引用数据类型,那么拷贝的对象会直接复制其引用数据类型,也就是说,原对象和引用对象共用一个内部对象。
深拷贝会完全复制整个对象,包括原对象中的内部对象。
public class Address implements Cloneable{
private String name;
// 省略构造函数、Getter&Setter方法
@Override
public Address clone() {
try {
return (Address) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
}
public class Person implements Cloneable {
private Address address;
// 省略构造函数、Getter&Setter方法
@Override
public Person clone() {
try {
// 浅拷贝
Person person = (Person) super.clone();
return person;
// 深拷贝
Person person = (Person) super.clone();
person.setAddress(this.address.clone());
return person;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
}
边栏推荐
- Apache kylin Adventure
- IGMP message (tcp/ip details volume 1/ Volume 2)
- (4) Googlenet replay
- 5-5配置Mysql复制 基于日志点的复制
- Solution of user and root forgetting password in virtual machine
- Project training of Software College of Shandong University rendering engine system radiation pre calculation (VIII)
- Getting started with JMeter
- 【光源实用案例】 UV-LED固化创新,让产线变得更丝滑
- Codeworks round 797 (Div. 3, cf1690)
- Explanation of socket principle (where, what and how to use it)
猜你喜欢

RARP summary (tcp/ip explanation volume 1/2)

Five models of software testing

Project training of Software College of Shandong University rendering engine system basic renderer (6)

【工具推荐】个人本地 markdown 知识图谱软件 Obsidian

Axure RP 9 for Mac(交互式产品原型设计工具)中文版

Unicom Network Management Protocol block diagram

Solution of user and root forgetting password in virtual machine

With a lamp inserted in the nostril, the IQ has risen and become popular in Silicon Valley. 30000 yuan is enough

5G新方案!升级现有的基站和UE模拟器至5G毫米波频段

Step by step to create a trial version of ABAP program containing custom screen
随机推荐
【光源实用案例】 UV-LED固化创新,让产线变得更丝滑
RARP summary (tcp/ip explanation volume 1/2)
Solutions to some problems of scuacm22 retreat competition before summer training
Find the number of cells (connectivity map, wide search, deep search)
C regular expression
国产CPLD中AG1280Q48进行开发的实践之一:思路分析
Decision tree classification and examples
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(五)
【架构优化过程思考】如何构建技术方案影响的评估能力
Application of postman-rest client plug-in
小飞页升级变智能修复Bug更快速了
Using the CSDN markdown editor
Applet: how to get the user's mobile number in the plug-in
Project training of Software College of Shandong University rendering engine system radiation pre calculation (VIII)
Glibc memory management model frees C library memory cache
Idea Encyclopedia (Reprinted)
C语言 分割bin文件程序
Getting started with JMeter
Codeworks round 797 (Div. 3, cf1690)
写代码也有本手俗手之分,而我们要善于发现妙手!