C#中抽象类的构造函数 [英] Constructor of an abstract class in C#

查看:34
本文介绍了C#中抽象类的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么可以在 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 {

     B(string myString) : base(myString){}

}

这篇关于C#中抽象类的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆