简体   繁体   English

静态类与受保护的构造函数

[英]Static Class vs Protected Constructor

I Am getting a warning message in my class, like我在课堂上收到一条警告消息,例如

在此处输入图片说明

Add a Protected constructor or the static` keyword to the class declaration在类声明中添加Protected constructor or the static` 关键字

Solution解决方案

The error is gone, after I tried both below ways, .在我尝试了以下两种方法后,错误消失了。

static class without constructor没有constructor static

public static class Program    {
    }

Non static class with protected using constructor使用构造函数protectedstatic

public  class Program
    {
        protected Program() { }
    }

Question:问题:

So What is the difference of Static Class vs Protected Constructor which is mentioned in my above solution?那么我上面的解决方案中提到的静态类与受保护的构造函数有什么区别? And which one is best to use?哪一种最好用?

A static class doesn't need an instance to access its members. static类不需要实例来访问其成员。 A static class cannot have instance members (eg public int MyNumber; is not allowed on a static class because only static members are allowed on a static class). static类不能有实例成员(例如public int MyNumber;在静态类上是不允许的,因为静态类只允许静态成员)。 Both instance and static members are allowed on a non-static class though.但是,非静态类中允许使用实例成员和静态成员。 A class with a protected constructor can only have an instance created by itself or something that inherits from it.具有protected构造函数的类只能有一个由它自己创建的实例或从它继承的东西。

public class Program
{
    protected Program()
    {
        // Do something.
    }

    public static Program Create()
    {
        // 100% Allowed.
        return new Program();
    }

    public void DoSomething()
    {

    }
}

public static class AnotherClass
{
    public static Program CreateProgram()
    {
        // Not allowed since Program's constructor is protected.
        return new Program();
    }
}

public class SubProgram : Program
{
    protected SubProgram()
    {
        // Calls Program() then SubProgram().
    }

    public new static Program Create()
    {
        // return new Program(); // We would need to move the SubProgram class INSIDE the Program class in order for this line to work.
        return new SubProgram();
    }
}

Program.Create();               // Can be called since Create is public and static function.
Program.DoSomething()           // Can't be called because an instance has not been instantiated.
var test = Program.Create();
test.DoSomething();             // Can be called since there is now an instance of Program (i.e. 'test').
AnotherClass.CreateProgram();   // Can't be called since Program's constructor is protected.
SubProgram.Create();            // Can be called since SubProgram inherits from Program.

As for performance, this distinction doesn't really have much to do with performance.至于性能,这种区别实际上与性能没有太大关系。

You probably only have static members in the class and the code analyser assumes that your intention is to not be able to create instances of the class so it is asking you to either make the class static您可能在类中只有静态成员,并且代码分析器假定您的意图是无法创建类的实例,因此它要求您将类设为静态

public static class Program {
    //...static members
}

or put a protected/private constructor或放置一个受保护/私有的构造函数

public class Program {
    protected Program { //OR private

    }

    //...static members
}

to prevent instances of that class from being initialized.以防止该类的实例被初始化。

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated.静态类与非静态类基本相同,但有一个区别:静态类不能被实例化。

Reference Static Classes and Static Class Members (C# Programming Guide)引用静态类和静态类成员(C# 编程指南)

The protected constructor means that only derived classes can call the constructor受保护的构造函数意味着只有派生类才能调用构造函数

and a private constructor wont allow any other classes to initialize the class with a private constructor并且私有构造函数不允许任何其他类使用私有构造函数初始化该类

A static constructor is called when the class type is instantiated.实例化类类型时会调用静态构造函数。 The protected constructor is called when an instance of a class is created.在创建类的实例时调用受保护的构造函数。 The protected part means only classes that inherit the class can call it.受保护部分意味着只有继承该类的类才能调用它。

Static Constructor: Called once when the class type is instantiated and is used to initialize static members.静态构造函数:类类型实例化时调用一次,用于初始化静态成员。 Does not create an instance of the class.不创建类的实例。

Protected Constructor: A constructor that can be called only by the class or a class that inherits it.受保护的构造函数:只能由类或继承它的类调用的构造函数。

The best practices for this is that you should have a static constructor for initializing static members and a protected constructor if you only want classes that inherit to be able to create an instance of your class.对此的最佳实践是,如果您只希望继承的类能够创建您的类的实例,则您应该有一个用于初始化静态成员的静态构造函数和一个受保护的构造函数。 You can have both.你可以两者兼得。

public class MyClass
{
    static readonly long _someStaticMember;
    private bool _param;
    static MyClass()
    {
        //Do Some Logic
        _someStaticMember = SomeValueCalculated;
    }
    protected MyClass(bool param)
    {
        _param = param;
    }
}
public class ChildClass: MyClass
{
    public ChildClass(bool param) : base(param);
}
public class NotChildClass
{
    public MyClass someObject = new MyClass(true); //Will Fail
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM