当前位置:网站首页>[Yuri crack man] brings you easy understanding - deep copy and shallow copy
[Yuri crack man] brings you easy understanding - deep copy and shallow copy
2022-07-26 03:11:00 【Chen Yikang】
Preface
Red Alert 2 Yuri is in revenge , There is a very powerful combat arm —— yuri , Enemy arms can be psychologically controlled through mind control , For our camp , Because a Yuri can only control three combat units at most , Therefore, a cloning design has been developed , You can clone multiple Yuri , Realize the control of multiple enemy combat units .
Shallow copy
What does this have to do with shallow copies ?
When technology is not advanced ( hypothesis ), Yuri and Yuri split people have such a characteristic , There will be empathy between them , Just imagine , If there are hundreds of thousands of Yuri crack people , Once a Yuri split man dies , It means the collapse of the entire military region ( Yuri has only one , There can be an infinite number of people ), Bring huge losses , This is similar to Shallow copy , modify , Properties of any copied object ( Or the subject ), Will affect other objects ;
The code is as follows :
// state
class Action{
public String action = " normal ";
}
// yuri
class Yuri implements Cloneable{// Mark the cloned Yuri real body
public String name = " yuri ";
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();// Throw an exception , Because it is Object class , Downward transformation requires mandatory type conversion
Yuri yuriclone2 =(Yuri) yuri.clone();
System.out.println(yuri);
System.out.println(yuriclone1);
System.out.println("-------------- Split line --------------");
yuri.action.action = " injured ";// One injured , Others were injured
System.out.println(yuri);
System.out.println(yuriclone1);
System.out.println(yuriclone2);
}
}Running results :

analysis :

Deep copy
What does this have to do with deep copy ?
With advanced technology ( hypothesis ), In cloning, Yuri will not be associated with other fissured people , empathy , Even if a cracked man dies in situ , Nor will it affect other disintegrators and Yuri's real body to fight , This is similar to deep copy , modify , Properties of any copied object ( Or the subject ), Will not affect other objects ;
The code is as follows :
// state
class Action implements Cloneable{
public String action = " normal ";
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
// yuri
class Yuri implements Cloneable{// Mark the cloned Yuri real body
public String name = " yuri ";
public Action action = new Action();
@Override
protected Object clone() throws CloneNotSupportedException {
Yuri tmpclone = (Yuri) super.clone();// The cloning of Yuri
tmpclone.action = (Action) this.action.clone();// Cloning of States
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();// Throw an exception , Because it is Object class , Downward transformation requires mandatory type conversion
Yuri yuriclone2 =(Yuri) yuri.clone();
System.out.println(yuri);
System.out.println(yuriclone1);
System.out.println(yuriclone2);
System.out.println("-------------- Split line --------------");
yuri.action.action = " injured ";// One injured , Others were injured
System.out.println(yuri);
System.out.println(yuriclone1);
System.out.println(yuriclone2);
}
}Running results :

analysis :


边栏推荐
- Swin Transformer【Backbone】
- canvas——绘制图片——动图制作
- STM - exti external interrupt learning notes
- GoLang日志编程系统
- LeetCode·每日一题·剑指 Offer || 115.重建序列·拓扑排序
- Win11隐藏输入法状态栏方法
- An article allows you to understand the relevance of cloud native containerization
- How to correctly calculate the CPU utilization of kubernetes container
- Programming example of STM32 state machine -- fully automatic washing machine (Part 1)
- 一篇文章让你理解 云原生 容器化相关
猜你喜欢

Safety margin of mass consumption

js中数组排序的方法有哪些

How to reinstall win7 system?

Win11 method of changing disk drive letter

Difference between soft link and hard link

Jenkins' study notes are detailed

经典面试问题——OOP语言的三大特征

Parallelloopbody in opencv

canvas——绘制曲线——挂钟,饼图,五角星

STM - exti external interrupt learning notes
随机推荐
这种动态规划你见过吗——状态机动态规划之股票问题(上)
Difference between soft link and hard link
How can users create data tables on Web pages and store them in the database
canvas——绘制图片——动图制作
STM32——DMA笔记
[sql] case expression
VR panoramic shooting and production of business center helps businesses effectively attract people
如何根据登录测试的需求设计测试用例?
STM32——串口学习笔记(一个字节、16位数据、字符串、数组)
canvas——心电图的设计,以及如何清理画布
实现一个方法,找出数组中的第k大和第m大的数字相加之和
[SQL] 自连接的用法
c语言分层理解(c语言函数)
Unknown-Aware Object Detection:Learning What You Don’t Know from Videos in the Wild(CVPR 2022)
STM32 - serial port learning notes (one byte, 16 bit data, string, array)
Continuous delivery and Devops are good friends
How to reinstall win7 system?
Hello World driver (II) - primary version
"Xiao Deng's view" the value brought by Siem to enterprises (II)
26 points that must be paid attention to for stability test
