如何克隆继承的对象? [英] How to clone an inherited object?
问题描述
我有一个瓷砖
用这种方法类:
公共对象的clone()
{
返回MemberwiseClone();
}
和其他类检查
从瓷砖
继承。
我也有一个板
类是列表与LT;瓷砖>
。我想克隆的板子,所以我写了这一点:
公板的clone()
{
变种b =新的董事会(宽,高);
的foreach(在此变种t)的b。增加(t.Clone());
回复B;
}
但它抛出一个错误:
无法从对象到Checkers.Tile
转换块引用>
现在我可以让
Tile.Clone
方法返回一个瓷砖
代替,但会在MemberwiseClone
复制子的附加属性? -检查
以及
和如果这不是问题,什么是上述
Board.Clone
法之间的语义差别呢?
$ b $采用b公板的clone()
{
(VAR毫秒=新的MemoryStream())
{
变种BF =新的BinaryFormatter();
bf.Serialize(MS,这一点);
ms.Position = 0;
回报(局)bf.Deserialize(MS);
}
}
由于他们肯定有我的节目有不同的影响即使当我打印板它的看起来的相同。我不觉得有什么正在的克隆的但引用返回。在
板
构造函数是这样的:公板(INT宽度= 8,INT高= 8)
{
this.width =宽度;
this.height =高度;
this.rowWidth =宽度/ 2;
this.Capacity = rowWidth *高度;
}
的
瓷砖
类实际上没有任何属性。该检查只是有两个枚举属性:公共枚举颜色{黑,白};
公共枚举类{文,景};
公共类检查:瓷砖
{
众彩颜色{搞定;组; }
公共类类{搞定;组; }
解决方案是的,的 MemberwiseClone 也将复制
检查
-only领域。 MemberwiseClone无法知道你的克隆
方法的返回类型;因此,它的行为不能依赖它
关于betweeen您的克隆实施差异和序列化:
MemberwiseClone
创建的浅拷贝的瓦片:如果瓷砖(或检查)引用了一些对象,该瓷砖的克隆仍引用的相同的对象(而不是它的副本)。
在另一方面,您的序列码是的众所周知实践以创建董事会的深拷贝的:相关对象的全树序列化和反序列化
当然,这只是如果你的瓷砖(或跳棋)包含引用类型的字段有差别。
I've got a
Tile
class with this method:public object Clone() { return MemberwiseClone(); }
And another class
Checker
that inherits fromTile
.I also have a
Board
class that is aList<Tile>
. I want to clone the board, so I wrote this:public Board Clone() { var b = new Board(width, height); foreach (var t in this) b.Add(t.Clone()); return b; }
But it throws an error:
cannot convert from 'object' to 'Checkers.Tile'
Now I can make the
Tile.Clone
method return aTile
instead, but then will theMemberwiseClone
copy the additional properties in the sub-Checker
as well?
If that's not the problem, what's the semantic difference between the above
Board.Clone
method and this?public Board Clone() { using (var ms = new MemoryStream()) { var bf = new BinaryFormatter(); bf.Serialize(ms, this); ms.Position = 0; return (Board)bf.Deserialize(ms); } }
Because they're definitely having different effects on my program, even though when I print the board it looks the same. I don't think something is being cloned but a reference is being returned. The
Board
ctor looks like this:public Board(int width = 8, int height = 8) { this.width = width; this.height = height; this.rowWidth = width / 2; this.Capacity = rowWidth * height; }
The
Tile
class actually doesn't have any properties. The checker just has two enums properties:public enum Color { Black, White }; public enum Class { Man, King }; public class Checker : Tile { public Color Color { get; set; } public Class Class { get; set; }
解决方案Yes, MemberwiseClone will also copy the
Checker
-only fields. MemberwiseClone cannot know the return type of yourClone
method; therefore, it's behaviour cannot depend on it.
About the difference betweeen your Clone implementation and the serialization:
MemberwiseClone
creates a shallow copy of the Tiles: If a Tile (or Checker) references some object, the Tile's clone still references the same object (rather than a copy of it).On the other hand, your serialization code is a well known practice for creating a deep copy of your board: The whole tree of dependent objects is serialized and deserialized.
Of course, this only makes a difference if your Tiles (or Checkers) contain fields with reference types.
这篇关于如何克隆继承的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!