Creating a cloned copy of subclass from baseclass

后端 未结 5 1707
鱼传尺愫
鱼传尺愫 2021-01-14 02:50

Consider this scenario:

public class Base
{
    public int i;
}

public class Sub : Base
{
    public void foo() { /* do stuff */}
}

And th

5条回答
  •  臣服心动
    2021-01-14 03:15

    Clone is a bad practice and your question is the reason for that (subclass cloning). In general, you should just use copy cotrs instead and have the subclass accept a parent as a parameter.

    public Base(){}

    public Base(Base pSource){}

    public Sub(){}

    public Sub(Base pSource, other parameters...){}

    public Sub(Sub pSource){}

提交回复
热议问题