C# - Making all derived classes call the base class constructor

前端 未结 4 756
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 17:15

I have a base class Character which has several classes deriving from it. The base class has various fields and methods.

All of my derived classes use the same base

相关标签:
4条回答
  • 2020-11-27 18:02

    A kind of alternative could be to rely on a Dependency Injection container to initialize your objects, that way the that reference to the base class (could be the call to the base constructor or another initializer method) would "externalized" to the DI container.

    I don't know if it makes sense to your case or not

    0 讨论(0)
  • 2020-11-27 18:08

    You do have to redeclare constructors, because they're effectively not inherited. It makes sense if you think of constructors as being a bit like static methods in some respects.

    In particular, you wouldn't want all constructors to be automatically inherited - after all, that would mean that every class would have a parameterless constructor, as object itself does.

    If you just want to call the base class constructor though, you don't need to write any code in the body of the constructor - just pass the arguments up to the base class as per Waleed's post.

    If your base class starts requiring more information, it's natural that you should have to change all derived classes - and indeed anything calling the constructors of those classes - because they have to provide the information. I know it can seem like a pain, but it's just a natural consequence of what constructors do.

    0 讨论(0)
  • 2020-11-27 18:12

    I had the same problem, and I solved it by replacing my constructor with a factory method like this:

    A is the parent class.

    public static T getChild<T>(int number) where T:A, new()
    {
        T child = new T();
        T._number = number;
        return child;
    
    }
    

    You can create a Child class with

    Child b = A.getChild<Child>(2);

    0 讨论(0)
  • 2020-11-27 18:14

    You can use the following syntax to call the base class constructor from the classes that derive from it:

    public DerivedClass() : base() {
        // Do additional work here otherwise you can leave it empty
    }
    

    This will call the base constructor first, then it will perform any additional statements, if any, in this derived constructor.

    Note that if the base constructor takes arguments you can do this:

    public DerivedClass(int parameter1, string parameter2) 
        : base(parameter1, parameter2) {
        // DerivedClass parameter types have to match base class types
        // Do additional work here otherwise you can leave it empty
    }
    

    You can find more information about constructors in the following page:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors

    In a derived class, if a base-class constructor is not called explicitly by using the base keyword, the default constructor, if there is one, is called implicitly.

    0 讨论(0)
提交回复
热议问题