[FxCop.设计规则]

http://blog.csdn.net/cajon/category/122838.aspx?PageNumber=1

1.     抽象类不应该拥有构造函数

原文引用:

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 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.

      }

   }

}

引起的原因:

一个公共抽象类型拥有一个公共的构造函数

描述:

构造函数被用来建立一个对象实例,但是你不能建立一个抽象类型的实例,抽象类型的构造函数就仅仅能够被它的继承类型使用。因此,为一个抽象类构造公共构造函数是一个错误的设计。

修复:

如果需要修复这个问题,可以声明这个构造函数为保护型,或者,声明这个类型不是一个抽象类型。

转载于:https://www.cnblogs.com/xioxu/archive/2008/05/06/1185497.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值