c# - Subclass of an Abstract class, always call Abstract constructor -


i have abstract class , child class

abstract class abstractclass {     public int x;      protected abstractclass(int x)     {         this.x = x;     } }  class childclass : abstractclass {     childclass(int x)         : base(x)     {     } } 

i have many child classes one, of classes call common constructor abstractclass1(int x) calling base(x) in constructors.

when want add new property constructor, example int y, have edit of classes , add y parameter in every child's constructor plus abstract class constructor.

is there way of doing this, considering know beforehand every child class using abstract class constructor , won't adding else ?

here's 1 idea: make abstractclassparameters class:

class abstractclassparameters {     public int x { get; private set; }     // add more parameters here     public abstractclassparameters(int x) {         this.x = x;         // add more initializers here     } } 

now can pass instance of abstractclassparameters abstract class constructor, , every subclass can same, passing through.

abstract class abstractclass {     public int x;      protected abstractclass(abstractclassparameters p)     {         this.x = p.x;                 } }  class childclass : abstractclass {     childclass(abstractclassparameters p) : base(p) { } } 

when add parameter, have edit abstractclassparameters class , abstractclass, not every subclass.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -