how to copy SubClass object in BaseClass copy constructor

前端 未结 3 697
清歌不尽
清歌不尽 2021-01-28 10:53

I would like to make copy of SubClass object in BaseClass constructor. I need that the following code execute correctly.

class BaseClass{
    BaseClass() {}
             


        
3条回答
  •  伪装坚强ぢ
    2021-01-28 11:28

    It's not possible. A constructor of class A gives you an instance of A, no way to circumvent this. Why not instantiate the subclass explicitly?

    Another possibility might involve a static factory method like:

    public static BaseClass create(BaseClass input) {
           // return BaseClass or subclass
    }
    

提交回复
热议问题