抽象类型不应具有构造函数
| TypeName | AbstractTypesShouldNotHaveConstructors |
| CheckId | CA1012 |
| Category | Microsoft.Design |
| Breaking Change | NonBreaking |
示例
下面的示例包含一个与该规则冲突的抽象类型以及一个可正确实现的抽象类型。
C#
using System; namespace DesignLibrary { public abstract class BadAbstractClassWithConstructor { // Violates rule: AbstractTypesShouldNotHaveConstructors. public BadAbstractClassWithConstructor() { // Add constructor logic here. } } public abstract class GoodAbstractClassWithConstructor { protected GoodAbstractClassWithConstructor() { // Add constructor logic here. } } }
本文讨论了抽象类型不应具有公共构造函数的设计原则,因为抽象类的构造函数只能被派生类调用,而不能直接实例化。建议将构造函数设为受保护的或避免声明为抽象类型。
9227

被折叠的 条评论
为什么被折叠?



