http://hi.baidu.com/sanlng/blog/item/7596a4cb69fafdfc52664f0a.html
一、浅拷贝
先看代码
//Country类
public class Country
{
string _Location = "" ;
public Country()
{
}
public string Location
{
get
{
return _Location ;
}
set
{
_Location = value ;
}
}
}
//Member类
public class Member
{
private Country TCountry = new Country() ;
string _Name = "" ;
public Member()
{
}
public string Name
{
get
{
return _Name ;
}
set
{
_Name = value ;
}
}
public string Country
{
get
{
return TCountry.Location ;
}
set
{
TCountry.Location = value ;
}
}
public object Clone()
{
return this.MemberwiseClone() ;
}
}
我们调用一下Member类,如下
Member tMember = new Member() ;
Member tMember1 = (Member)tMember.Clone() ;
tMember.Country = "中国" ;
tMember1.Country = "美国" ;
Response.Write("tMember的位置:" + tMember.Country + "<br>") ;
Response.Write("tMember1的位置:" + tMember1.Country) ;
最后的输出结果为:
tMember的位置:美国
tMember1的位置:美国
发现问题的所在了吗?为什么tMember对象与tMember1对象的Country属性的值一样呢?我们可是为这两个对象分别设置了它们的Country属性呀!这就是浅拷贝的问题。
注意Country属性的实现,在Member类中,该属性是用一个Country对象来存储其值的,对象是引用类型的,利用浅拷贝新建出来的对象,新对象与原对象共享引用类型的数据。那怎么才能让新对象与原对象拥有各自的引用类型(即不共享引用类型的数据)呢?这就是深拷贝与浅拷贝的区别!
二、深拷贝
以下代码利用序列化与反序列化实现对象的Clone,下面的类没有实现接口ICloneable,如果你一定要实现这个接口也可以,但具体的Clone方法实现方式,估计和下面代码实现的方式差不多。这里需要说明一点就是,在实现深拷贝的时候,在Clone()方法中你可以直接返回一个新的比如Memeber对象,但建议你不要这样做,因为直接return new Member()返回的是一个没有任何状态的新对象(虽然这样可以使新对象与原有对象不共享任何数据),除非对象的状态对你没用。
/// <summary>
/// Member类
/// </summary>
[SerializableAttribute()]
public class Member
{
private Country TCountry = new Country() ;
string _Name = "" ;
public Member()
{
}
public string Name
{
get
{
return _Name ;
}
set
{
_Name = value ;
}
}
public string Country
{
get
{
return TCountry.Location ;
}
set
{
TCountry.Location = value ;
}
}
public object Clone()
{
IFormatter formatter = new BinaryFormatter();
System.IO.MemoryStream t = new MemoryStream() ;
formatter.Serialize( t , this) ;
t.Seek(0, SeekOrigin.Begin);
object obj = (object)formatter.Deserialize(t) ;//反序列化得到当前对象的一个副本
return obj ;
}
}
/// <summary>
/// Country类
/// </summary>
[SerializableAttribute()]
public class Country
{
string _Location = "" ;
public Country()
{
}
public string Location
{
get
{
return _Location ;
}
set
{
_Location = value ;
}
}
public object Clone()
{
return this.MemberwiseClone() ;
}
}
对上面的类进行调用
Member tMember = new Member() ;
Member tMember1 = (Member)tMember.Clone() ;
tMember.Country = "中国" ;
tMember1.Country = "美国" ;
Response.Write("tMember的位置:" + tMember.Country + "<br>") ;
Response.Write("tMember1的位置:" + tMember1.Country) ;
程序的输出结果为:
tMember的位置:中国
tMember1的位置:美国
这样,我们要的效果就出来了。因为新Clone出来的对象与原对象不共享任何数据,从Country属性的值就可以看出来。如果你有更好的办法,不妨交流一下。
http://www.soft6.com/tech/5/58335.html
C++类对象的复制-拷贝构造函数(深拷贝,浅拷贝)
C++类对象的复制-拷贝构造函数(深拷贝,浅拷贝),进一步理解类成员的操作!
在学习这一章内容前我们已经学习过了类的构造函数和析构函数的相关知识,对于普通类型的对象来说,他们之间的复制是很简单的,例如:
int a = 10;
int b =a;
自己定义的类的对象同样是对象,谁也不能阻止我们用以下的方式进行复制,例如:
#include <iostream>
usingnamespacestd;
classTest
{
public:
Test(inttemp)
{
p1=temp;
}
protected:
intp1;
};
voidmain()
{
Test a(99);
Test b=a;
}
http://www.yimudi.cn/portal/article/277709240.html
深拷贝与浅拷贝
- 来自:歪酷博客 作者:Kenneth ’s Blog 时间:2007-12-30
深拷贝与浅拷贝探析
1. 深拷贝是指源对象与拷贝对象互相独立,其中任何一个对象的改动都不会对另外一个对象造成影响。举个例子,一个人名叫张三,后来用他克隆(假设法律允许)了另外一个人,叫李四,不管是张三缺胳膊少腿还是李四缺胳膊少腿都不会影响另外一个人。比较典型的就是value(值)对象,如预定义类型int32,double,以及结构(struct),枚举(enum)等。