当前位置:网站首页>【尤里复裂人】带你轻松理解——深拷贝和浅拷贝
【尤里复裂人】带你轻松理解——深拷贝和浅拷贝
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);
}
}运行结果:

分析:


边栏推荐
- JVM memory model parsing
- Nahamcon CTF 2022 babyrev reverse analysis
- Matlab simulation of vertical handover between MTD SCDMA and TD LTE dual networks
- assert _ Aligns
- Software testing post: Ali has three sides. Fortunately, he has made full preparations and has been offered
- QT signal transmission between multi-level objects signal transmission between multi-level nested class objects
- The difference between the world wide web, the Internet and the Internet
- OxyCon 2022 网络抓取前沿大会即将开启!
- 万维网、因特网和互联网的区别
- Swin Transformer【Backbone】
猜你喜欢

朋友刚学完自动化测试就拿25Koffer,我功能测试何时才能到头?
![[sql] usage of self connection](/img/92/92474343b4b4e6ea60453b4799cb55.jpg)
[sql] usage of self connection

图像识别(六)| 激活函数

MySQL tutorial: MySQL database learning classic (from getting started to mastering)
![[SQL] CASE表达式](/img/05/1bbb0b5099443f7ce5f5511703477e.png)
[SQL] CASE表达式

Influence of middle tap change on ZVS oscillation circuit

Case: using kept+haproxy to build a Web Cluster

Vofa+ serial port debugging assistant

Image recognition (VII) | what is the pooling layer? What's the effect?

The LAAS protocol elephant of defi 2.0 is the key to revitalizing the development of defi track
随机推荐
Safety margin of mass consumption
The LAAS protocol elephant of defi 2.0 is the key to revitalizing the development of defi track
JVM内存模型解析
ES6 set and map
Keyboardtraffic, a tool developed by myself to solve CTF USB keyboard traffic
软件测试岗:阿里三面,幸好做足了准备,已拿offer
Matlab simulation of vertical handover between MTD SCDMA and TD LTE dual networks
Image recognition (VII) | what is the pooling layer? What's the effect?
Usage of '...' in golang
MySQL教程:MySQL数据库学习宝典(从入门到精通)
[SQL] CASE表达式
STM32——串口学习笔记(一个字节、16位数据、字符串、数组)
测试/开发程序员老了怎么办?挥之不去残酷的事实......
FPGA_ Initial use process of vivado software_ Ultra detailed
How to reinstall win7 system?
规范自己debug的流程
File operation (I) -- File introduction and file opening and closing methods
c语言分层理解(c语言函数)
Shardingsphere data slicing
文件操作(一)——文件简介与文件的打开方式和关闭
