当前位置:网站首页>面试:什么是浅拷贝、深拷贝?
面试:什么是浅拷贝、深拷贝?
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();
}
}
}
边栏推荐
- (四)GoogleNet复现
- [practical case of light source] UV-LED curing innovation makes the production line more smooth
- Great God cracked the AMD k6-2+ processor 22 years ago and opened the hidden 128KB L2 cache
- Remote control of other computers -- detailed tutorial
- Find the number of cells (connectivity map, wide search, deep search)
- Explanation of socket principle (where, what and how to use it)
- 小飞页升级变智能修复Bug更快速了
- CUDA out of memory or brokenpipeerror: [errno 32] broken pipe or oserror: [winerror 1455] solution to the problem that the page file is too small
- 记一篇IT培训日记067-好人感恩,坏人无错
- VIM installation and common commands
猜你喜欢
Scanpy (VI) analysis and visualization of spatial transcriptome data
Axure RP 9 for MAC (interactive product prototyping tool) Chinese version
Explanation of socket principle (where, what and how to use it)
Fiddler packet capturing (mobile app)
[tool recommendation] personal local markdown knowledge map software
Application of postman-rest client plug-in
借助SpotBugs将程序错误扼杀在摇篮中
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(七)
Scanpy(六)空间转录组数据的分析与可视化
Raccourci vers le nouvel environnement du carnet de notes Jupiter
随机推荐
学习记录[email protected]一文搞懂canvas
In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]
办公室VR黄片,骚操作!微软HoloLens之父辞职!
redis 通用命令
How to use grafana to easily realize OVL data visualization
Fiddler packet capturing (mobile app)
Introduction and download website of common data of GIS, remote sensing, hydrology and Geography (2), supplementary~
Method reference instance method reference
借助SpotBugs将程序错误扼杀在摇篮中
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(七)
(四)GoogleNet复现
Import and export steps of SQL Server 2008
Step by step steps to create an ABAP program with a custom screen
Golang collaboration scheduling (I): Collaboration Status
Idea Encyclopedia (Reprinted)
Development status of China's pig breeding industry in 2021 and comparative analysis of key enterprises: 671million pigs were sold [figure]
【架构优化过程思考】如何构建技术方案影响的评估能力
C regular expression
鼻孔插灯,智商上升,风靡硅谷,3万就成
Axure RP 9 for MAC (interactive product prototyping tool) Chinese version