简体   繁体   English

C# - 静态类,私有构造函数,抽象类 - 都阻止实例创建 - 使用哪一个?

[英]C# - Static class,Private Constructor,abstract class - all prevent instance creation-which one to use?

I am bit confused in the usage of Static class, Private constructor and abstract class 我对Static类,Private构造函数和抽象类的使用感到困惑

to prevent instance creation.( Confused about the alternatives). 防止实例创建。(对备选方案感到困惑)。

What is the scenario that fit the best for each ? 什么是最适合每种情况的方案?

That depends on your needs. 这取决于您的需求。

  • Static class may be considered "a bunch of methods" - you would use it, if you just need to group some methods, sample usage: MathHelpers, with methods like Sin, Cos, ConvertXToY (or to host extension methods). 静态类可以被认为是“一堆方法” - 你会使用它,如果你只需要对一些方法进行分组,样本用法:MathHelpers,使用Sin,Cos,ConvertXToY(或主机扩展方法)等方法。

  • Private constructor - this one you would use, when you want to be able to control how the object is created, for example, if you want to make sure, that those objects can only be created by your static methods. 私有构造函数 - 当您希望能够控制对象的创建方式时,您将使用此构造函数,例如,如果要确保这些对象只能由静态方法创建。 An example: 一个例子:


class Robot
{
 public string Name { get; }
 private Robot() 
 { 
   // some code
 }

 public static Robot CreateAndInitRobot(string name)
 {
   Robot r = new Robot();
   r.Name = name;
   return r;
 }
}
  • Abstract classes - Those you should use, when you are defining some abstract object, that shouldn't been initialized, because it's incomplete / abstract, and you want to further specialize it (by inheriting from it). 抽象类 - 当你定义一些抽象对象时应该使用的那些,它们不应该被初始化,因为它是不完整/抽象的,你想进一步专门化它(通过继承它)。

You use a static class when you only have static members. 只有静态成员时才使用静态类。 It doesn't work for any scenario when you want to inherit the class. 当您想要继承该类时,它不适用于任何场景。

A private constructor ensures that only code inside the class itself can create instances of it, ie a static method or an instance method in an already existing instance. 私有构造函数确保只有类本身内的代码才能创建它的实例,即现有实例中的静态方法或实例方法。 Even if you inherit from the class, it still has to call the creation process of the base class, so there is no way around it. 即使你从类继承,它仍然需要调用基类的创建过程,所以没有办法解决它。

An abstract class is useless for preventing instance creation. 抽象类对于防止实例创建是无用的。 You have to inherit the class to use it for anything, and you can add whatever constructor you like to the descendant class. 您必须继承该类才能将其用于任何内容,并且您可以将任何您喜欢的构造函数添加到后代类中。

Static classes cannot be used as a base class for inheritance and can only contain static members. 静态类不能用作继承的基类,只能包含静态成员。

Abstract classes cannot be instantiated at all and are useful only as a base class for inheritance. 抽象类根本无法实例化,仅作为继承的基类。 They can provide abstract memebers (with no implementation) that the derived class must override, they can provide virtual members (with default implementation) that the derived class may override, or they can provide regular (sealed) members that provide common functionality. 它们可以提供派生类必须覆盖的抽象成员(没有实现),它们可以提供派生类可以覆盖的虚拟成员(具有默认实现),或者它们可以提供提供通用功能的常规(密封)成员。

Private constructors (typically found in non-abstract, non-static classes) prevent the class from being instantiated with no parameters/initialization. 私有构造函数(通常在非抽象的非静态类中找到)阻止在没有参数/初始化的情况下实例化类。 They are most useful when the class requires specific information during initialization/construction to be in a valid state. 当类在初始化/构造期间要求特定信息处于有效状态时,它们最有用。

An abstract class means that you must inherit from the class to have something to instantiate. 抽象类意味着您必须从类继承才能实例化。 It's normally used where the base functionality is common, but where some specialisation will be provided in the derived class which cannot be defaulted - in other words, you have to provide your own behaviour. 它通常用于基本功能很常见的地方,但是派生类中会提供一些不能默认的专门化 - 换句话说,你必须提供自己的行为。

A private constructor was the pre .NET 2 way of preventing somebody from instantiating a class using new - in .NET 2, the static constructor was introduced. 私有构造函数是阻止某人使用new实例化类的.NET 2之前的方法 - 在.NET 2中引入了静态构造函数。 This would be used where you want to have some form of factory method, eg 这将用于您想要某种形式的工厂方法,例如

public static class MyClass
{
  static MyClass() {}
  private static MyClass LoadFromDatabase(int id)
  {
    // Get the data here....
  }
  public static MyClass LoadByKey(int id)
  {
    MyClass c = new MyClass();
    return c.LoadFromDatabase(id);
  }
}

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

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