Why can't I create an abstract constructor on an abstract C# class?

后端 未结 8 2080
无人及你
无人及你 2020-11-28 08:08

I am creating an abstract class. I want each of my derived classes to be forced to implement a specific signature of constructor. As such, I did what I would have done has I

相关标签:
8条回答
  • 2020-11-28 08:48

    not sure if this helps - but i feel this would be a solution to your problem:

    public class A
    {
      public A(int a, int b)
      {
        DoSomething(int a, int b);
      }
    
      virtual public void DoSomething(int a, int b)
      {
    
      }
    }
    
    public class B : A
    {
      override public void DoSomething(int a, int b)
      {
        //class specific stuff
      }
    }
    

    with the result that you can call a constructor with the required arguments in any derived class with the correct behaviour.

    0 讨论(0)
  • 2020-11-28 08:49

    You cannot have an abstract constructor because abstract means you must override it in any non-abstract child class and you cannot override a constructor.

    If you think about it, this makes sense, since you always call the constructor of the child class (with the new operator) and never the base class.

    Generally speaking, the only way in C# to enforce a specific constructor signature is by using the new() generic constraint, which enforces the existence of a parameterless constructor for the type parameter.

    0 讨论(0)
  • 2020-11-28 08:52

    Although you can't override constructors, and therefore can't define an abstract constructor, you can place an abstract factory method in your abstract base class. All the derived classes would need to override that.

    public abstract class A 
    { 
        abstract A MakeAInstance(int a, int b); 
    } 
    
    public class B : A 
    { 
        // Must implement:
        override A MakeAInstance(int a, int b) {
            // Awesome way to create a B instance goes here
        }
    } 
    
    0 讨论(0)
  • 2020-11-28 08:56

    You cannot enforce constructor signature, as each derived class may (must!) define its own constructor(s), and they may take any parameters they like.

    If you need to pass a given set of variables to an object of a derived class, define an abstract method which needs to be implemented by derived classes. If the classes do not implement the abstract method, you will get a compiler error.

    0 讨论(0)
  • Multiple reasons:

    1) Constructors are not inherited thus you cannot override them.

    2) The constructor is a static member function since it dont need a specific instance to be called. Abstract implies "virtual" which means that the implementation can vary depending on how a specific instance is subclassed, which is the opposite of the intention of the meaning of the "static" keyword.

    0 讨论(0)
  • 2020-11-28 09:01

    Change that constructor in class A to

    protected A(int a, int b)
    {
        // Some initialisation code here
    }
    

    Then your subclasses will have to use it, as there is no default constructor.

    They can, however, still change the actual signature of the constructor. There is no way of forcing a subclass to use a specific signature for its constructor as far as I know. I'm pretty sure constructors can't be abstract.

    What exactly do you need this for? We might be able to suggest a work around for this.

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