抽象类和构造函数 [英] Abstract class and constructor

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

问题描述

由于无法实例化抽象类,为什么在抽象类中仍允许使用构造函数?

As an abstract class cannot be instantiated, why is a constructor still allowed inside the abstract class?

public abstract class SomeClass 
 {  
     private string _label;

     public SomeClass(string label)  
     {  
         _label=label;
     }  
}


推荐答案

构造函数派生类的任何一个仍必须在抽象类中调用构造函数。如果根本不指定任何构造函数,则所有派生类都将只使用编译器提供的默认无参数类。

Constructors of any derived class still have to call a constructor in the abstract class. If you don't specify any constructors at all, all derived classes will just have to use the default parameterless one supplied by the compiler.

构造函数-但在这种情况下,公共实际上等效于受保护。

It absolutely makes sense to have a constructor - but "public" is really equivalent to "protected" in this case.

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

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