简体   繁体   English

静态类和私有构造函数

[英]Static Class and Private Constructor

I am not able to understand this. 我无法理解这一点。 I tried doing a small example in VS2010 as below. 我尝试在VS2010中做一个小例子,如下所示。

"Creating a static class is therefore much the same as creating a class that contains only static members and a private constructor" “因此,创建静态类与创建仅包含静态成员和私有构造函数的类非常相似”

Does this statement means a class with private constructor and one or more static methods like below is static class ? 此语句是否表示具有私有构造函数的类,并且下面的一个或多个静态方法是静态类? I know we call a class static only when static keyword is mentioned in class. 我知道只有在类中提到static关键字时才调用类静态。

Also, we cannot inherit the below class and also we can't instantiate this class right ? 另外,我们不能继承下面的类,也不能实例化这个类吧?

public class Base
{
    private Base() { Console.WriteLine(" I am from normal Base constructor"); }
    static void NewMethod() { Console.WriteLine("Hey I am from Static Base"); }
    public void New() { } 
}

Creating a static class is therefore much the same as creating a class that contains only static members and a private constructor 因此,创建静态类与创建仅包含静态成员和私有构造函数的类非常相似

This statement is attempting to get across the right idea but failing to do so. 这句话试图找到正确的想法,但没有这样做。 A static class is not like a class with only static members and a private constructor. 静态类是喜欢,只有静态成员和私有构造函数的类。 Here's a class with static members and private constructor: 这是一个包含静态成员和私有构造函数的类:

class X
{
    private X() {}
    public static X Y() { return new X(); }
}

But that's not at all like a static class! 但这根本不像一个静态类! The author of that statement seems to think that having a private constructor prevents you from making instances, but of course it does not. 该声明的作者似乎认为拥有私有构造函数会阻止您创建实例,但当然不会。

A more accurate statement would be: 更准确的说法是:

Creating a static class is much like creating a class that is both abstract and sealed, and contains no instance members. 创建一个静态类就像创建一个既抽象又密封的类,并且不包含实例成员。

And in fact, when the C# compiler generates the code for a static class, that's precisely what it does: it marks the class as both abstract (so it cannot be instantiated directly) and sealed (so that it cannot be extended). 事实上,当C#编译器为静态类生成代码时,它正是它的作用:它将类标记为抽象(因此无法直接实例化)和密封(因此无法扩展)。

I note that it is not legal for you to declare a class both abstract and sealed yourself; 我注意到你宣布一个既抽象又自己密封的课程是不合法的; the only way to do so in C# is to make a static class. 在C#中这样做的唯一方法是创建一个静态类。

I will bring the misleading sentence to the attention of the MSDN documentation managers. 我将把这个误导性的句子提请MSDN文档管理员注意。 Thanks for pointing it out. 谢谢你指出来。

What it means is that: 这意味着:

public static class Foo
{
    public static void Bar() { }
}

is essentially the same as 与...基本相同

public class Foo
{
    private Foo() { }
    public static void Bar() { }
}

because if the class only has a private constructor, it cannot be instantiated outside the class. 因为如果类只有一个私有构造函数,则无法在类外部实例化它。

"Does this statement mean a class with private constructor and one or more static methods like below is a static class?" “这个语句是否意味着具有私有构造函数的类和一个或多个静态方法(如下所示)是一个静态类?”

The answer is No, and one difference is explained in exactly the next sentence after the one you are citing from MSDN : 答案是否定的,在您从MSDN引用的那个句子之后的下一句中完全解释了一个区别:

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. 使用静态类的优点是编译器可以检查以确保不会意外添加实例成员。


It means that you will get a compiler error in Class2 shown below. 这意味着您将在下面显示的Class2中获得编译器错误。

public class Class1
{
    private Class1() { }
    public static void Method() { }
    private string member; // valid, but pointless
}

public static class Class2
{
    public static void Method() { }
    private string member; // error CS0708
}

More important, although Class1 has a private constructor, it may still be instantiated: 更重要的是,虽然Class1有一个私有构造函数,但它仍然可以实例化:

public class Class1
{
    private Class1() { }

    private static Class1 instance = new Class1();

    public static Class1 Instance
    {
        get { return instance; }
    }
}

A static class on the other hand, may never be instantiated. 另一方面,静态类可能永远不会被实例化。

You can not inherit as there is no public constructor, only a private constructor exists. 您不能继承,因为没有公共构造函数,只存在私有构造函数。 For the same reason you can not create an instance. 出于同样的原因,您无法创建实例。

Within the scope of this question, they can be seen as the same. 在这个问题的范围内,它们可以被视为相同。 Can you call it a static class; 你能称它为静态类吗? I think officially you do not since it is not marked as static in the class definition. 我认为你没有,因为它没有在类定义中标记为static But in the perspective of functionality you may call it static . 但从功能的角度来看,你可以称之为static

With your example, the only way the New() method can be called is if you add another method to return a Base instance since Base can't be instantiated by another class. 在您的示例中,可以调用New()方法的唯一方法是添加另一个方法来返回Base实例,因为Base不能由另一个类实例化。 Not having that extra method makes it functionally the same as a static class. 没有那个额外的方法使它在功能上与静态类相同。

Where did you get the statement: 你在哪里得到的声明:

Creating a static class is therefore much the same as creating a class that contains only static members and a private constructor 因此,创建静态类与创建仅包含静态成员和私有构造函数的类非常相似

What it is trying to say is that you can not create an instance of a static class. 它试图说的是你不能创建一个静态类的实例。

The way to create a static class is to use the static keyword. 创建静态类的方法是使用static关键字。 All members in the class must also be static. 班上的所有成员也必须是静态的。

public static class MyStaticClass
{
      static MyStaticClass() { /* Constructor.  Optional. */ }
      public static void MyMethod() { ... }
      public static int MyProperty{ get; set; }
}

Note again, you can not create an instance of this class. 请再次注意,您无法创建此类的实例。 The following call will not compile: 以下调用将无法编译:

new MyStaticClass();

A static class can never be instantiated, participate in inheritance, or use interfaces. 永远不能实例化静态类,参与继承或使用接口。 But a class, even with a private constructor can still inherit, where a static class cannot. 但是一个类,即使使用私有构造函数仍然可以继承,静态类不能继承。 And a class with a private constructor can still instantiate an instance of itself. 具有私有构造函数的类仍然可以实例化其自身的实例。 A static cannot do that. 静态无法做到这一点。 So NO they are not the same. 所以不,他们不一样。

I do not understand why Microsoft at times still doesn't understand what it created, because they are the ones that wrote that description (as Eric Lippert mentioned)? 我不明白为什么微软有时仍然不明白它创造了什么,因为他们是那些写这种描述的人(正如Eric Lippert所说)?

The other confusion is whether a Static Constructor is Private or Public? 另一个困惑是静态构造函数是私有还是公共? That would help explain behavior because like the default public constructor in every class, there is also a default constructor for every static class. 这有助于解释行为,因为像每个类中的默认公共构造函数一样,每个静态类都有一个默认构造函数。 To me that implies the static constructor would be public, but it is not. 对我来说暗示静态构造函数是公开的,但事实并非如此。 It cannot be accessed or called, and can be overwritten. 它无法访问或调用,可以覆盖。

Because the static class is translated when compiled into an abstract class that's sealed, that suggests its static constructor really is just another default public class constructor without parameters that can be called only once. 因为静态类在编译成密封的抽象类时被翻译,这表明它的静态构造函数实际上只是另一个默认的公共类构造函数,没有可以只调用一次的参数。 I could be wrong. 我错了。 But if that's the case, the whole private constructor explanation makes no sense. 但如果是这样的话,整个私有构造函数的解释都没有意义。

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

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