Copying the contents of a base class from a derived class

后端 未结 10 1398
粉色の甜心
粉色の甜心 2021-01-01 17:19

I currently have a derived class and a base class. How can I make the base class of the derived class equal to a base class that I have? Will a shallow copy work?



        
10条回答
  •  礼貌的吻别
    2021-01-01 17:47

    If I understand you correctly, this will work:

    class Derived : Base
    {
        // all the code you had above, plus this:
    
        public Derived(Base toCopy)
        {
            this.name = toCopy.name;
            this.address = toCopy.address;
        }
    }
    
    Derived d = new Derived(b);
    

提交回复
热议问题