浅克隆和深度克隆

本文通过具体示例对比浅克隆与深克隆的区别。浅克隆仅复制对象本身,而深克隆会递归复制所有引用对象。通过Table类的实现展示了如何实现这两种克隆方式,并展示了不同情况下克隆的效果。

浅克隆: 浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。

深克隆:深复制把要复制的对象所引用的对象都复制了一遍。下面用一个例子来看一下:

public class Point implements Cloneable {
private int x;
private int y;
public Point(){}
public Point(int x,int y){
this.x=x;
this.y=y;

}
public void setX(int x){this.x=x;}
public void setY(int y){this.y=y;}

public int getX(){return x;}
public int gety(){return y;}

public Object clone()throws CloneNotSupportedException{
return super.clone();
}
/**
* @param args
*/

public static void main(String[] args) throws CloneNotSupportedException{
// TODO 自动生成方法存根
Table table=new Table();
table.setCenter(new Point(2,3));
Point yuanshi=table.getCenter();

Table clontable=(Table)table.clone();
Point cloncenter=clontable.getCenter();
cloncenter.setX(4);
cloncenter.setY(21);

System.out.print(cloncenter.getX());
System.out.print(yuanshi.getX());


}

}

以上定义了可以克隆的类,以及在main函数中进行的测试。下面的类是关键所在:

public class Table implements Cloneable{
private Point center;

public void setCenter(Point center){
this.center=center;
}
public Point getCenter(){
return center;
}

public Object clone() throws CloneNotSupportedException{
Table table=(Table) super.clone();
if (this.center!=null){
table.center=(Point)center.clone();
}
return table;
}
}

Table类中,如果将红色代码去掉,就属于浅克隆,加上就是深克隆,可以将对象复制出来,两个对象在操作的过程中不会相互影响。

去掉红色代码输出结果是:4 4

加上后输出结果是:4 2

C#简单变色龙游戏 /// /// 创建一副52张的新牌 /// /// public static System.Collections.ArrayList Creat() { System.Collections.ArrayList list = new System.Collections.ArrayList(); for (int i = 3; i <= 6; i++) { for (int j = 1; j <= 13; j++) { Poker p = new Poker(); switch (j) { case 1: p = new Poker((PokerStyle)i, "A", j, 11); break; case 2: p = new Poker((PokerStyle)i, j.ToString(), j, 10); break; case 11: p = new Poker((PokerStyle)i, "J", j, 11); break; case 12: p = new Poker((PokerStyle)i, "Q", j, 2); break; case 13: p = new Poker((PokerStyle)i, "K", j, 3); break; default: p = new Poker((PokerStyle)i, j.ToString(), j, j); break; } list.Add(p); } } return list; } /// /// 洗牌 /// /// /// public static System.Collections.ArrayList shuffle(System.Collections.ArrayList list)//洗牌 { System.Random r = new Random(); for (int i = 0; i <= 7; i++) { int rightOrRight = r.Next(1, 3); Poker[] leftPoker = new Poker[list.Count / 2]; Poker[] rightPoker = new Poker[list.Count % 2 == 0 ? list.Count / 2 : list.Count / 2 + 1]; list.CopyTo(0, leftPoker, 0, list.Count / 2); list.CopyTo(list.Count / 2, rightPoker, 0, list.Count % 2 == 0 ? list.Count / 2 : list.Count / 2 + 1); if (rightOrRight == 1)//左边插右边 { list.Clear(); for (int j = 0; j <= leftPoker.Length - 1; j++) { list.Add(rightPoker[j]); list.Add(leftPoker[j]); } if (leftPoker.Length != rightPoker.Length) { list.Add(rightPoker[rightPoker.Length - 1]); } } else { list.Clear(); for (int j = 0; j <= leftPoker.Length - 1; j++) { list.Add(leftPoker[j]); list.Add(rightPoker[j]); } if (leftPoker.Length != rightPoker.Length) { list.Add(rightPoker[rightPoker.Length - 1]); } } } return list; }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值