当前位置:网站首页>【尤里复裂人】带你轻松理解——深拷贝和浅拷贝
【尤里复裂人】带你轻松理解——深拷贝和浅拷贝
2022-07-26 03:05:00 【陈亦康】
前言
红警2尤里复仇中,有个一个非常强大作战兵种——尤里,可以通过心灵控制将敌方兵种进行心理控制,为我方阵营所用,由于一个尤里最多只能控制三个作战单位,所以研究出了一种克隆设计,可以克隆多个尤里,实现对敌方多个作战单位的控制。
浅拷贝
那这和浅拷贝有什么关系呢?
在科技不先进的情况下(假设),尤里和尤里复裂人有这样一个特性,他们之间会存在感同身受,试想,如果有几十万个尤里复裂人,某一个尤里复裂人一旦战死,意味着整个军区的覆灭(尤里只有一个,复裂人可以有无限个),带来巨大的损失,这便类似于浅拷贝,修改,任何一个被拷贝的对象的属性(亦或是主体),都会影响到其他对象;
代码如下:
//状态
class Action{
public String action = "正常";
}
//尤里
class Yuri implements Cloneable{//标记被克隆的尤里真身
public String name = "尤里";
public Action action = new Action();
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "Yuri{" +
"name='" + name + '\'' +
", action='" + action.action + '\'' +
'}';
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Yuri yuri = new Yuri();
Yuri yuriclone1 =(Yuri) yuri.clone();//要抛一个异常,由于是Object类,向下转型要进行强制类型转化
Yuri yuriclone2 =(Yuri) yuri.clone();
System.out.println(yuri);
System.out.println(yuriclone1);
System.out.println("--------------分割线--------------");
yuri.action.action = "受伤";//一个受伤,其他跟着受伤
System.out.println(yuri);
System.out.println(yuriclone1);
System.out.println(yuriclone2);
}
}运行结果:

分析:

深拷贝
那这和深拷贝有什么关系呢?
在科技先进的情况下(假设),在克隆时不会造成尤里与其他复裂人相关联,感同身受,即使一个复裂人原地去世,也不影响其他复裂人以及尤里真身进行作战,这便类似于深拷贝,修改,任何一个被拷贝的对象的属性(亦或是主体),都不会影响到其他对象;
代码如下:
//状态
class Action implements Cloneable{
public String action = "正常";
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
//尤里
class Yuri implements Cloneable{//标记被克隆的尤里真身
public String name = "尤里";
public Action action = new Action();
@Override
protected Object clone() throws CloneNotSupportedException {
Yuri tmpclone = (Yuri) super.clone();//对尤里的克隆
tmpclone.action = (Action) this.action.clone();//对状态的克隆
return tmpclone;
}
@Override
public String toString() {
return "Yuri{" +
"name='" + name + '\'' +
", action='" + action.action + '\'' +
'}';
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Yuri yuri = new Yuri();
Yuri yuriclone1 =(Yuri) yuri.clone();//要抛一个异常,由于是Object类,向下转型要进行强制类型转化
Yuri yuriclone2 =(Yuri) yuri.clone();
System.out.println(yuri);
System.out.println(yuriclone1);
System.out.println(yuriclone2);
System.out.println("--------------分割线--------------");
yuri.action.action = "受伤";//一个受伤,其他跟着受伤
System.out.println(yuri);
System.out.println(yuriclone1);
System.out.println(yuriclone2);
}
}运行结果:

分析:


边栏推荐
- The source of everything, the choice of code branching strategy
- STM32 - DMA notes
- 图像识别(六)| 激活函数
- How to speed up matrix multiplication
- [translation] announce Vites 13
- Usage of fuser and lsof
- I hope you can help me with MySQL
- Safety margin of mass consumption
- Vofa+ serial port debugging assistant
- Keyboardtraffic, a tool developed by myself to solve CTF USB keyboard traffic
猜你喜欢

JVM内存模型解析

Vofa+ serial port debugging assistant

Win11麦克风权限的开启方法

Multithreaded programming

Nahamcon CTF 2022 babyrev reverse analysis

STM32 - DMA notes

Win11大小写提示图标怎么关闭?Win11大小写提示图标的关闭方法

规范自己debug的流程
![[C language] deeply understand integer lifting and arithmetic conversion](/img/5c/21d0df424c034721c64b0653edc483.png)
[C language] deeply understand integer lifting and arithmetic conversion

Autojs cloud control source code + display
随机推荐
Matlab simulation of vertical handover between MTD SCDMA and TD LTE dual networks
Service gateway (zuul)
VOFA+ 串口调试助手
Software testing post: Ali has three sides. Fortunately, he has made full preparations and has been offered
I hope you can help me with MySQL
Study notes of pytorch deep learning practice: convolutional neural network (Advanced)
对于稳定性测试必需关注的26点
JVM memory model parsing
[C language] deeply understand integer lifting and arithmetic conversion
cmd cpm 命令汇总
The source of everything, the choice of code branching strategy
Quick check of OGC WebGIS common service standards (wms/wmts/tms/wfs)
Longest Substring Without Repeating Characters
Teach you to rely on management hand in hand
GoLang 抽奖系统 设计
C language layered understanding (C language function)
Shardingsphere data slicing
Wechat official account mutual aid, open white groups, and small white newspaper groups to keep warm
How to reinstall win7 system?
Win11隐藏输入法状态栏方法
