[FxCop.设计规则]1. 抽象类不应该拥有构造函数
2005-05-24 21:03
357 查看
1. 抽象类不应该拥有构造函数
原文引用:
http://www.gotdotnet.com/team/fxcop/docs/rules.aspx?version=unstable&url=/Design/AbstractTypesShouldNotHaveConstructors.html Abstract types should not have constructors
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 an abstract type, an abstract type with a public constructor is incorrectly designed. How to Fix Violations To fix a violation of this rule, either make the constructor protected, or do not declare the type as abstract. When to Exclude Messages Do not exclude a message from this rule. Example Code The following example contains an abstract type that violates this rule, and an abstract type that is correctly implemented. [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. } } } |
引起的原因:(Cause)
一个公共抽象类型拥有一个公共的构造函数描述:(Rule Description )
构造函数被用来建立一个对象实例,但是你不能建立一个抽象类型的实例,抽象类型的构造函数就仅仅能够被它的继承类型使用。因此,为一个抽象类构造公共构造函数是一个错误的设计。修复:(How to Fix Violations)
如果需要修复这个问题,可以声明这个构造函数为保护型,或者,声明这个类型不是一个抽象类型。相关文章推荐
- [FxCop.设计规则]1. 抽象类不应该拥有构造函数
- [FxCop.设计规则]2. 程序集应该拥有一个有效的强名称
- [FxCop.设计规则]2. 程序集应该拥有一个有效的强名称
- [FxCop.设计规则]2. 程序集应该拥有有效的强命名
- [FxCop.设计规则]8. 也许参数类型应该是基类型
- [FxCop.设计规则]11. 不应该使用默认参数
- [FxCop.设计规则]11. 不应该使用默认参数
- [FxCop.设计规则]10. 类型应该被声明在命名空间中
- [FxCop.设计规则]7. 集合类应该实现泛型接口
- [FxCop.设计规则]10. 类型应该被声明在命名空间中
- [FxCop.设计规则]1. 抽象类型不应具有构造函数
- [FxCop.设计规则]8. 也许参数类型应该是基类型
- [FxCop.设计规则]5. 避免命名空间中包含太少的类型
- [FxCop.设计规则]16. 不要在封闭类中声明虚成员
- 抽象类设计规则,特点及举例
- [FxCop.设计规则] 3. 避免定义空的接口
- 你应该知道的网页设计中的规则和禁忌
- [FxCop.设计规则]13. 定义自定义属性参数的访问属性
- FxCop 设计规则
- [FxCop.设计规则]3. 避免定义空的接口