正确的方式来实现ICloneable [英] Proper way to implement ICloneable
问题描述
什么是类层次实施 ICloneable
的正确方法是什么?说我有一个抽象类 DrawingObject
。另一个抽象类的RectangularObject
从 DrawingObject
继承。随后有多个具体类,如图形
,文本
,圆
等等,所有从的RectangularObject
。我想实施 ICloneable
在 DrawingObject
然后再进行下来的层次,每个层次复制可用的属性,并呼吁家长的克隆
在一个新的水平。
What is the proper way of implementing ICloneable
in a class hierarchy? Say I have an abstract class DrawingObject
. Another abstract class RectangularObject
inherits from DrawingObject
. Then there are multiple concrete classes like Shape
, Text
, Circle
etc. that all inherit from RectangularObject
. I want to implement ICloneable
on DrawingObject
and then carry it down the hierarchy, copying available properties at each level and calling parent's Clone
at the next level.
但问题是,既然前两个类是抽象的,我不能在克隆()
方法来创建自己的对象。因此,我必须复制在每个具体的类属性复制过程。还是有更好的办法吗?
The problem however is that since the first two classes are abstract, I cannot create their objects in the Clone()
method. Thus I must duplicate the property-copying procedure in each concrete class. Or is there a better way?
推荐答案
您可以轻松地创建带有对象
的保护方法<一个肤浅的克隆href="http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone%28v=vs.110%29.aspx">MemberwiseClone.
You can easily create a superficial clone with object
's protected method MemberwiseClone.
例:
public abstract class AbstractCloneable : ICloneable
{
public object Clone()
{
return this.MemberwiseClone();
}
}
如果你并不需要像深拷贝什么,你就不必在子类做任何事情。
If you don't need anything like a deep copy, you will not have to do anything in the child classes.
的MemberwiseClone方法创建一个新的对象,然后复制当前对象的非静态字段到新的对象创建一个浅表副本。如果一个字段是值类型,执行字段的位逐位拷贝。如果字段是引用类型,引用复制的,但被引用的对象不是;因此,原来的对象及其克隆指代相同的对象。
The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object.
如果您在克隆逻辑需要更多的智慧,你可以添加一个虚拟的方法来处理参考文献:
If you need more intelligence in the cloning logic, you can add a virtual method to handle references :
public abstract class AbstractCloneable : ICloneable
{
public object Clone()
{
var clone = (AbstractCloneable) this.MemberwiseClone();
HandleCloned(clone);
return clone;
}
protected virtual HandleCloned(AbstractCloneable clone)
{
//Nothing particular in the base class, but maybe usefull for childs.
//Not abstract so childs may not implement this if they don't need to.
}
}
public class ConcreteCloneable : AbstractCloneable
{
protected override HandleCloned(AbstractCloneable clone)
{
//Get wathever magic a base class could have implemented.
base.HandleCloned(clone);
//Clone is of the current type.
ConcreteCloneable obj = (ConcreteCloneable) clone;
//Here you have a superficial copy of "this". You can do wathever
//specific task you need to do.
//e.g.:
obj.SomeReferencedPropertie = this.SomeReferencedPropertie.Clone();
}
}
这篇关于正确的方式来实现ICloneable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!