• [FxCop.设计规则]1. 抽象类不应该拥有构造函数


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

    原文引用:

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

       }

    }

    引起的原因:(Cause)

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

    描述:(Rule Description )

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

    修复:(How to Fix Violations)

    如果需要修复这个问题,可以声明这个构造函数为保护型,或者,声明这个类型不是一个抽象类型。
  • 相关阅读:
    Linux_C_C++
    01玩转数据结构_09_线段树Segment Tree(区间树)
    线性代数
    概率与统计
    矩阵快速幂—— 构造矩阵
    2020百度之星程序设计大赛复赛
    Codeforces Round #662 (Div. 2)
    2020百度之星程序设计大赛初赛二
    2020百度之星程序设计大赛初赛一
    安全参考监视器支持例程(转的微软的)
  • 原文地址:https://www.cnblogs.com/Cajon/p/161724.html
Copyright © 2020-2023  润新知