Why can't I create an abstract constructor on

2020-01-25 06:16发布

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 wanted to force them to implement a method, I made an abstract one.

public abstract class A
{
    abstract A(int a, int b);
}

However I get a message saying the abstract modifier is invalid on this item. My goal was to force some code like this.

public class B : A
{
    public B(int a, int b) : base(a, b)
    {
        //Some other awesome code.
    }
}

This is all C# .NET code. Can anyone help me out?

Update 1

I wanted to add some things. What I ended up with was this.

private A() { }

protected A(int a, int b)
{
    //Code
}

That does what some folks are saying, default is private, and the class needs to implement a constructor. However that doesn't FORCE a constructor with the signature A(int a, int b).

public abstract class A
{
    protected abstract A(int a, int b)
    {


    }
}

Update 2

I should be clear, to work around this I made my default constructor private, and my other constructor protected. I am not really looking for a way to make my code work. I took care of that. I am looking to understand why C# does not let you do this.

8条回答
forever°为你锁心
2楼-- · 2020-01-25 06:43

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.

查看更多
我命由我不由天
3楼-- · 2020-01-25 06:44

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.

查看更多
ゆ 、 Hurt°
4楼-- · 2020-01-25 06:44

hope this will help someone newb as i am i was looking to make a "normal" public class with a ctor that takes argument and then i needed to make it a child class so i needed an empty ctor for it.

i didnt know this before: if you make a protected ctor then the child class sees it but the rest of the program dont see it (i guess my confusion is since in asp.net i see all the protected in the aspx page which inherits from the cs...)

查看更多
We Are One
5楼-- · 2020-01-25 06:53

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.

查看更多
ゆ 、 Hurt°
6楼-- · 2020-01-25 07:01

All subclasses always can specify their own constructor as long as they call a constructor of the superclass - so there is no way of forcing the a class to have a specific constructor (At least, that is the way it works in Java). You could see of using a factory pattern will get you somewhere - you could define a factory method in an interface - but you'll need a separate factory class as your abstract base class does not know the actual class of the object that needs to be created.

However: maybe adding a more concrete example of the issue you are having might prompt other/better responses. Are you looking for some generic instantiation code, or are you concerned specific settings on the abstract base class have to be done?

If you are just concerned about initialization that has to be done by the abstract class, create a method to do so, and document the usage of that method.

查看更多
戒情不戒烟
7楼-- · 2020-01-25 07:02

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
    }
} 
查看更多
登录 后发表回答