当前位置:网站首页>[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 :


边栏推荐
- 文件操作(一)——文件简介与文件的打开方式和关闭
- canvas——矩形的绘制——柱状图的制作
- Golang 中‘...‘的用法
- Software testing post: Ali has three sides. Fortunately, he has made full preparations and has been offered
- LeetCode·83双周赛·6128.最好的扑克手牌·模拟
- Opencv 以指定格式保存图片
- [NOIP2001 普及组] 最大公约数和最小公倍数问题
- Hello World driver (II) - primary version
- 【尤里复裂人】带你轻松理解——深拷贝和浅拷贝
- 在混合云中管理数据库:八个关键注意事项
猜你喜欢

Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 1)

Autojs cloud control source code + display

Detailed explanation of extended physics informedneural networks paper

Machine learning foundation plan 0-2: what is machine learning? What does it have to do with AI?

如何正确计算 Kubernetes 容器 CPU 使用率

FPGA_ Initial use process of vivado software_ Ultra detailed

Win11麦克风权限的开启方法

Win11 method of changing disk drive letter

Win11更改磁盘驱动器号的方法
![[sql] case expression](/img/05/1bbb0b5099443f7ce5f5511703477e.png)
[sql] case expression
随机推荐
Quick check of OGC WebGIS common service standards (wms/wmts/tms/wfs)
My friend took 25koffer as soon as he learned automation test. When will my function test end?
STM——EXTI外部中断学习笔记
hello world驱动(二)-初级版
FPGA_ Initial use process of vivado software_ Ultra detailed
手把手教你依赖管理
Win11 hide input method status bar method
Opencv 以指定格式保存图片
LeetCode·83双周赛·6128.最好的扑克手牌·模拟
[C Advanced] deeply explore the storage of data (in-depth analysis + interpretation of typical examples)
Win11麦克风权限的开启方法
Dataframe sorting: datetime format splitting; Delete a specific line; Group integration.
文件操作(一)——文件简介与文件的打开方式和关闭
How to reinstall win7 system?
Keyboardtraffic, a tool developed by myself to solve CTF USB keyboard traffic
STM32 - serial port learning notes (one byte, 16 bit data, string, array)
YOLOv3: An Incremental Improvement
Golang log programming system
The source of everything, the choice of code branching strategy
LeetCode·每日一题·919.完全二叉树插入器·层次遍历·BFS
