Pet*_*ger 1 c# constructor types
首先,我有这样的事情:
class Parent
{
int x;
public Parent(int _x)
{
x = _x
}
}
class Child1: Parent
{
int y;
public Child1(int _y):base(_y)
{
y=_y;
}
}
class Child2: Parent
{
int z;
public Child2(int _z):base(_z)
{
z=_z;
}
}
Run Code Online (Sandbox Code Playgroud)
一个简单的父子层次结构.然后,我有一个实际上充满了Child1和Child2的List.我想制作列表中每个对象的副本,我想首先制作一个新的项目作为副本.
但是,如果我这样做:
foreach(Parent p in list)
dictionaryOfCopies.Add(p, new Parent(p.x));
Run Code Online (Sandbox Code Playgroud)
然后字典将充满父母,而不是儿童1和儿童2.有没有办法在不知道对象的特定类型的情况下调用作为其父类型键入的对象的构造函数?
一种方法是在对象上实现ICloneable接口,让每个实例克隆自己.
class Parent : ICloneable
{
int x;
public Parent(int _x)
{
x = _x
}
public virtual object Clone()
{
return new Parent(x);
}
}
class Child1 : Parent
{
int y;
public Child1(int _y) : base(_y)
{
y = _y;
}
public override object Clone()
{
return new Child1(y);
}
}
class Child2 : Parent
{
int z;
public Child2(int _z) : base(_z)
{
z = _z;
}
public override object Clone()
{
return new Child2(z);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,你会像这样使用它:
foreach(Parent p in list)
{
dictionaryOfCopies.Add(p, p.Clone() as Parent);
}
Run Code Online (Sandbox Code Playgroud)
作为魔鬼的拥护者,我在ICloneable界面上看到的一个批评是它不是类型安全的.如果这对您来说很烦人,您仍然可以采用相同的想法,但实现自己的Clone方法版本Parent而不是返回object.