如何在以抽象类为根的树层次结构中更正实现ICloneable?

时间:2015-01-24 00:58:08

标签: c# abstract-class class-hierarchy icloneable

给定树层次结构,假设它如下:

abstract class Person : ICloneable
...
sealed class Student : Person
...

我想实现ICloneable接口。在Student.Clone方法中,我希望做类似的事情:

{
     Student clonedStudent = base.Clone() as Student;
     clonedStudent.x1 = this.x1;
     return clonedStudent
}

因为Person是抽象的,我无法在Person.Clone()方法中创建Person,所以我无法返回克隆的Person,所以我无法克隆Person。

我想到的最好的答案是重载Person类中的Clone()方法以接收Person,克隆并返回它。然后在Student.Clone实现中调用此重载来克隆人员的相关字段。 像这样:

//In the Person class:
public abstract object Clone();
protected Person Clone(Person clonedPerson)
{
    // Clone all person's fields
    return clonedPerson:
}
//In the Student class:
public override object Clone()
{
    Student clonedStudent = base.Clone(new Student()) as Student;
    // Clone all student's fields
    return clonedStudent
}     

当然,如果上述任何类需要在其构造函数中构建任何逻辑,那么这个解决方案就无用了。有没有想过实现更好的?

我认为这是一个更普遍的子问题,所以答案很适合大型超集。

1 个答案:

答案 0 :(得分:2)

由于您的Student类从基类(Person)继承属性/字段,因此无需克隆person对象。

解决方案是不在抽象基类中实现Clone()方法,而是将其抽象化并强制执行从其继承的任何子类。为了克隆,您可以简单地实例化Student类的新实例,并使用克隆构造函数填充基本属性。看看我的例子。我不确定这是否有任何帮助。

class Program
    {
        static void Main(string[] args)
        {
            Student studentA = new Student(1000, "Defense Against the Dark Arts", "Harry", "Potter", 25);
            Student studentB = (Student)studentA.Clone();
        }
    }

    public abstract class Person : ICloneable
    {
        public string FirstName { get; set; }
        public string Surname { get; set; }

        private int SomePrivateVariable { get; set; }

        public Person()
        {

        }

        public Person(string firstName, string surname, int privateVariableDefault)
        {
            this.FirstName = firstName;
            this.Surname = surname;
            this.SomePrivateVariable = privateVariableDefault;
        }

        public Person(Person original)
        {
            this.FirstName = original.FirstName;
            this.Surname = original.Surname;
            this.SomePrivateVariable = original.SomePrivateVariable;
        }

        public abstract object Clone();
    }

    public sealed class Student : Person
    {
        public int StudentId { get; set; }
        public string CourseTitle { get; set; }

        public Student()
        {

        }

        //Constructor with all the fields, passed down to the base class
        public Student(int studentId, string courseTitle, string firstName, string surname, int baseVariableDefault)
            : base(firstName, surname, baseVariableDefault)
        {
            this.StudentId = studentId;
            this.CourseTitle = courseTitle;
        }

        //A clone constructor which takes an object of the same type and populates internal 
        //and base properties during construction
        public Student(Student original)
            : base(original)
        {
            this.FirstName = original.FirstName;
            this.Surname = original.Surname;
            this.StudentId = original.StudentId;
            this.CourseTitle = original.CourseTitle;
        }

        public override object Clone()
        {
            Student clone = new Student(this);    
            return clone;
        }
    }