可复制的,在派生类中 [英] Cloneable in Derived Classes

查看:108
本文介绍了可复制的,在派生类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类 A B 从派生 A

Assume I have a class A, and B which derives from A:

class A : ICloneable
{
    public object Clone() {...}
}

class B : A, ICloneable
{
    public object Clone() {...}
}

这使得

'B.Clone()' hides inherited member 'A.Clone()'. Use the new keyword if hiding was intended.



警告。

warning.

(1)什么是建议的方法是什么?使用或声明 A.Clone()虚拟覆盖 b

(1) What is the suggested way? using new or declaring A.Clone() as virtual and override in B?

(2)有在 A 部分成员并妥善 A.Clone克隆(),有一个简单的方法来克隆它们在 B.Clone()或做我必须显式地克隆它们 B.Clone()也?

(2) If there are some members in A and properly cloned in A.Clone(), is there an easy way to clone them in B.Clone() or do I have to explicitly clone them in B.Clone() also?

推荐答案

如果你有机会到源(我猜是这里的情况),那么绝对声明为虚拟和覆盖它。如果隐藏基克隆可能是一个坏主意。如果任何代码不的知道的是它的工作有一个 B ,那么将触发的错误的克隆方法。不能返回正确的克隆

If you have access to your source (which I'm guessing is the case here) then absolutely declare it as virtual and override it. If hide the base Clone with new might be a bad idea. If any code doesn't know that it's working with a B, then it will fire the wrong clone method and not return a proper clone.

关于物业转让,或许是考虑实施的拷贝构造函数和每个级别可以处理自己的克隆:

Regarding the assignment of properties, perhaps consider implementing copy constructors and each level can handle its own cloning:

    public class A : ICloneable
    {
        public int PropertyA { get; private set; }

        public A()
        {

        }

        protected A(A copy)
        {
            this.PropertyA = copy.PropertyA;
        }

        public virtual object Clone()
        {
            return new A(this);
        }
    }

    public class B : A, ICloneable
    {
        public int PropertyB { get; private set; }

        public B()
        {

        }

        protected B(B copy)
            : base(copy)
        {
            this.PropertyB = this.PropertyB;
        }

        public override object Clone()
        {
            return new B(this);
        }
    }

每个拷贝构造函数调用基拷贝构造函数传递自行关闭连锁,链条。每个继承级别副本的属性直接属于它

Each copy constructor calls the base copy constructor passing itself down the chain. Each inheritance level copies the properties belonging to it directly.

编辑:如果您使用关键字隐藏基本实现,这里有可能发生的事情为例。与样品实现(在它的脸看起来罚款)

If you use the new keyword to hide the base implementation, here's an example of what might happen. With a sample implementation (which on the face of it looks fine)

public class A : ICloneable
{
    public int PropertyA { get; protected set; }

    public object Clone()
    {
        Console.WriteLine("Clone A called");
        A copy = new A();
        copy.PropertyA = this.PropertyA;
        return copy;
    }
}

public class B : A, ICloneable
{
    public int PropertyB { get; protected set; }

    public new object Clone()
    {
        Console.WriteLine("Clone B called");
        B copy = new B();
        copy.PropertyA = this.PropertyA;
        copy.PropertyB = this.PropertyB;
        return copy;
    }
}



但是,当你使用它:

But when you use it:

B b = new B();
A a = b;
B bCopy = (B)a.Clone();
//"Clone A called" Throws InvalidCastException! We have an A!

这篇关于可复制的,在派生类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆