简体   繁体   English

静态类别 具有私有构造函数的类以及所有静态属性和方法?

[英]Static Class Vs. Class with private constructor and all static properties and methods?

When I create utility classes I typically create a class that has a private constructor and exposes all of it's methods and properties as static. 当我创建实用程序类时,我通常会创建一个具有私有构造函数的类,并将其所有方法和属性公开为static。 What's the best approach for this? 对此最好的方法是什么? What's the difference between the way I do or creating a static class? 我的方式或创建静态类有什么区别?

Static classes are automatically sealed, so people can't inherit and override their behavior. 静态类是自动密封的,因此人们无法继承和覆盖它们的行为。

That is the only real difference (unless there is something special in the IL) 这是唯一真正的区别(除非IL中有特殊内容)

So if you use a static class, you save yourself the trouble of making the constructor private, and declaring the class sealed. 因此,如果您使用静态类,则可以省去将构造函数设为私有的麻烦,并声明类密封。

I would add, that defining a class as static, is "self-documenting" code. 我想补充说,将类定义为静态是“自我记录”代码。 Users of your library will know that this class should not be instantiated, and only has static values. 您的库的用户将知道不应该实例化此类,并且只有静态值。

A static class can never be instantiated. 永远不能实例化静态类。 No way, no how. 没有办法,没有办法。

A non-static class with a private constructor but all static methods can be abused in various ways - inheritance, reflection, call the private constructor in a static factory - to instantiate the class. 具有私有构造函数但非所有静态方法的非静态类可以以各种方式滥用 - 继承,反射,调用静态工厂中的私有构造函数 - 来实例化类。

If you never want instantiation, I'd go with the static class. 如果你不想要实例化,我会选择静态类。


Edit - Clarification for FosterZ's comment 编辑 - 澄清FosterZ的评论

Say you have this utility class: 假设您有此实用程序类:

public class Utility
{
    public static string Config1 { get { return "Fourty Two"; } }

    public static int Negate(int x) { return -x; }

    private Utility() { }      
}

If another developer isn't clear on its intent, they could do this: 如果其他开发人员不清楚其意图,他们可以这样做:

public class Utility
{
    public static string Config1 { get { return "Fourty Two"; } }
    public int Config2 { get; set; }

    public static int Negate(int x) { return -x; }

    private Utility() { }

    /// Get an instance of Utility     
    public static Utility GetUtility()
    {
        return new Utility();
    }
}   

Now you have a Frankenstein class. 现在你有一个弗兰肯斯坦班。 Some of its features require instantiation and some don't. 它的一些功能需要实例化,有些则不需要。 Maybe that's what you want but maybe it's not. 也许这就是你想要的,但也许不是。 You could prevent this with code review, but why not make your intentions explicit in code? 您可以通过代码审查来防止这种情况,但为什么不在代码中明确表达您的意图? Marking the class as static eliminates any possible confusion. 将类标记为static消除了任何可能的混淆。 You can't instantiate a static class or inherit from it. 您无法实例化静态类或从中继承。

In addition to the previous answers: The compiler won't allow non-static members on static classes and produces and error. 除了之前的答案:编译器不允许静态类上的非静态成员产生和错误。 This may help a little bit to not accidently add non-static members. 这可能有助于一点点不会意外添加非静态成员。

you can pass object of a class that has private constructor as a parameter to any method but you can't do the same with static class. 你可以将具有私有构造函数的类的对象作为参数传递给任何方法,但是你不能对静态类做同样的事情。 This is the major difference. 这是主要的区别。

Also I'm going to go with the private constructor never being called by the static methods. 此外,我将继续使用静态方法调用的私有构造函数。 So any initialisation in there would be wasted... But that's just a theory. 因此,任何初始化都会浪费......但这只是一种理论。

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

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