有没有一种方法可以通过类的层次结构来分配属性
本文关键字:层次结构 属性 分配 可以通过 方法 一种 有没有 | 更新日期: 2023-09-27 18:20:28
请查看以下类的层次结构:
Person [age, country, hair color, eyes color..]
Adult [job, car..]
Man [favorite beer..]
Woman [purse..]
Child [school, favorite toy]
Boy
Girl [doll]
每个派生类都有特定的属性:例如,成年人可能有工作,但孩子没有。一个女孩可能有一个最喜欢的娃娃和一所学校的名字。一个男孩也有学校的名字,但他没有最喜欢的娃娃。
我想实现一个克隆Boy
(重新运行具有完全相同属性的Boy
对象)的方法。我不想返回Boy
并手动设置从Child
和Person
继承的所有属性。
注:Child
、Adult
、Person
为抽象类。
注2:所有这些人都有复杂的引用,我不想复制,在某些情况下,我只想复制引用的ID,但这应该手动完成,所以我需要对克隆的对象进行一些控制。
我在Person
中想到了一个虚拟方法,它在每个子类中都被覆盖,但由于Person
无法实例化,我不确定如何实现这种行为。
取3。。。
解决方案1:
我更喜欢的方法是使用ICopyTo。我认为它比其他任何东西都好,因为它强制有一个合适类型的对象来复制。它还可以同时进行克隆和复制。更易于维护。
使用界面也有助于做正确的事情。别忘了打电话给基地。复制到。。。
此外,我们可以说CopyTo是Fluent接口的一部分
public interface ICopyTo<T>
{
T CopyTo(T target);
}
public abstract class Person : ICopyTo<Person>, ICloneable
{
public Person CopyTo(Person person)
{
person.Age = Age;
person.Country = Country;
return person;
}
public abstract object Clone();
public int Age { get; set; }
public string Country { get; set; }
}
public abstract class Adult : Person, ICopyTo<Adult>, ICloneable
{
public Adult CopyTo(Adult adult)
{
base.CopyTo(this);
adult.Car = Car;
return adult;
}
public string Car { get; set; }
}
public class Man : Adult, ICopyTo<Man>, ICloneable
{
public Man CopyTo(Man man = null)
{
if (man == null)
{
man = new Man();
}
base.CopyTo(this);
man.Beer = Beer;
return man;
}
public string Beer { get; set; }
public override object Clone()
{
return CopyTo();
}
}
public class Woman : Adult, ICopyTo<Woman>, ICloneable
{
public Woman CopyTo(Woman woman = null)
{
if (woman == null)
{
woman = new Woman();
}
base.CopyTo(this);
woman.Purse = Purse;
return woman;
}
public string Purse { get; set; }
public override object Clone()
{
return CopyTo();
}
}
public class Test
{
public static void Go()
{
Man man1 = new Man() {Age = 10, Beer = "Bud", Country = "Canada"};
Man man2 = new Man();
man1.CopyTo(man2); // Real copy
Woman woman1 = new Woman() {Age = 32, Country = "USA", Purse = "Anything"};
Woman woman2 = woman1.CopyTo(); // Cloning
List<Person> adults = new List<Person>();
adults.Add(man1);
adults.Add(man2);
adults.Add(woman2);
Person person0 = adults[0].Clone() as Person;
Person person1 = adults[1].Clone() as Person;
Person person2 = adults[2].Clone() as Person;
}
}
解决方案2:(接近解决方案1,但ICloneable仅在基类)
public interface ICopyTo<T>
{
T CopyTo(T target);
}
public abstract class Person : ICopyTo<Person>, ICloneable
{
public virtual Person CopyTo(Person person)
{
if (person == null)
{
throw new ArgumentNullException("person can't be null");
}
person.Age = Age;
person.Country = Country;
return person;
}
public object Clone()
{
return CopyTo(null);
}
public int Age { get; set; }
public string Country { get; set; }
}
public abstract class Adult : Person, ICopyTo<Adult>, ICloneable
{
public Adult CopyTo(Adult adult)
{
if (adult == null)
{
throw new ArgumentNullException("adult can't be null");
}
base.CopyTo(this);
adult.Car = Car;
return adult;
}
public override Person CopyTo(Person person)
{
return CopyTo(person as Adult);
}
public string Car { get; set; }
}
public class Man : Adult, ICopyTo<Man>
{
public Man CopyTo(Man man = null)
{
if (man == null)
{
man = new Man();
}
base.CopyTo(this);
man.Beer = Beer;
return man;
}
public override Person CopyTo(Person person)
{
return CopyTo(person as Man);
}
public string Beer { get; set; }
}
public class Woman : Adult, ICopyTo<Woman>
{
public Woman CopyTo(Woman woman = null)
{
if (woman == null)
{
woman = new Woman();
}
base.CopyTo(this);
woman.Purse = Purse;
return woman;
}
public override Person CopyTo(Person person)
{
return CopyTo(person as Woman);
}
public string Purse { get; set; }
}
public class Test
{
public static void Go()
{
Man man1 = new Man() {Age = 10, Beer = "Bud", Country = "Canada"};
Man man2 = new Man();
man1.CopyTo(man2); // Real copy
Woman woman1 = new Woman() {Age = 32, Country = "USA", Purse = "Anything"};
Woman woman2 = woman1.CopyTo(); // Cloning
List<Person> adults = new List<Person>();
adults.Add(man1);
adults.Add(man2);
adults.Add(woman2);
Person person0 = adults[0].Clone() as Person;
Person person1 = adults[1].Clone() as Person;
Person person2 = adults[2].Clone() as Person;
}
}
解决方案3:
public static T Clone<T>(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable.", "source");
}
// Don't serialize a null object, simply return the default for that object
if (Object.ReferenceEquals(source, null))
{
return default(T);
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
不必走浅层/深层克隆的道路(正如Marco所指出的),您可以使用自己的Clone
函数来实现这一点。
class Person
{
public Person(Person rhs) // cctor
{
Age = rhs.Age;
}
public int Age { get; set; }
public abstract Person Clone();
}
public class Adult : Person
{
public Adult(Adult rhs) : base(rhs)
{
JobType = rhs.JobType;
}
public JobType Job { get; set; }
public override Person Clone() { return new Adult(this); }
}
MemberwiseClone()
方法可能是您想要的:https://msdn.microsoft.com/en-us/library/system.object.memberwiseclone(v=vs.110).aspx
请注意,您需要手动克隆引用类型的类的任何成员,因为上述函数将只克隆它们的引用,并且生成的Boy克隆成员将引用与这些成员的原始实例相同的实例。
如果您的大多数或所有成员都是引用类型,ICloneable是一个更稳健的解决方案:https://msdn.microsoft.com/en-us/library/system.icloneable(v=vs.110).aspx
你可以在基类上使用简单的,如果你想在上层类中重写,你可以使用virtual。
public abstract class Person : ICloneable {
public virtual object Clone() {
//all upper classes is a Person object
person = (Person)Activator.CreateInstance(this.GetType());
person.job = this.job;
// ect more properties Lazy or Deep clone
person.blah = this.blah;
return person;
}
}
public abstract class Adult : Person {
override object Clone() {
Adult adult = (Adult) base.Clone();
adult.job = this.job;
}
}