请问C#中 抽象类有没有构造方法 构造方法中能不能带参数

来源:百度知道 编辑:UC知道 时间:2024/05/23 22:15:56

abstract (C# 参考)
abstract 修饰词可用於类别、方法、属性、索引子 (Indexer) 和事件。在类别宣告里使用 abstract 修饰词,表示该类别只是当做其他类别的基底类别而已。成员如果标记为抽象,或是包含在抽象类别 (Abstract Class) 内,则必须由衍生自此抽象类别的类别实作这个成员。

抽象类根本没有实作,又要衍生类来实作,构造函数又不可以继承,在抽象类中加构造函数和参数都是没有用的.我试过加上也不会报错.

抽象类是不可以被直接构造的,所以他的构造函数没有意义。很多语言中都不允许为抽象类添加构造函数,但是C#允许为抽象类添加构造函数。不过这是不推荐的做法,因为这样做会破坏设计原则!
以下为FXCop设计规则中对抽象类含有构造函数的警告:

Abstract types should not have constructors

TypeName:

AbstractTypesShouldNotHaveConstructors

CheckId:

CA1012

Category:

Microsoft.Design

Message Level:

CriticalWarning

Certainty:

95%

Breaking Change:

NonBreaking

Cause: A public type is abstract and has a public constructor.

Rule Description

Constructors on abstract types can only be called by derived types. Because public constructors create instances of a type, and you cannot create instances of