1. 程式人生 > WINDOWS開發 >抽象類為什麼可以有建構函式?- Constructor of an abstract class in C#(轉載)

抽象類為什麼可以有建構函式?- Constructor of an abstract class in C#(轉載)


Why is it possible to write constructor for an abstract class in C#?
As far as I know we can‘t instantiate an abstract class.. so what is it for?
You can‘t instantiate the class,right?


Because there might be a standard way you want to instantiate data in the abstract class. That way you can have classes that inherit from that class call the base constructor.

public abstract class A
{

    private string data;

    protected A(string myString)
    {
        data = myString;
    }

}

public class B : A
{
    public B(string myString) : base(myString)
    {

    }

}

原文連結