初始化程序和静态初始化程序之间的区别?

发布于 2024-08-09 13:33:51 字数 316 浏览 7 评论 0原文

当我在 Asp.Net 中使用 XmlDOM 时,有一个这样的模式:“XmlReader reader = XmlReader.Create()”。

后来我多次遇到相同的模式。

我喜欢知道静态构造函数和“new ClassName()”构造函数之间有什么区别(我不确定我是否使用了正确的术语来描述我的意思)。

我不是在问XmlReader.Create()。是的,我想了解的是为什么我会使用静态构造函数而不是?它会提供什么样的方面?我可以使用静态构造函数做但不能使用 new 关键字构造函数做的事情

When I was working with XmlDOM in Asp.Net, there was a pattern like this : `XmlReader reader = XmlReader.Create()".

And then I encountered the same pattern several times later.

I like to know what's the difference between Static Constructor and "new ClassName()" Constructor (I am not sure if I am using right terms to describe what I mean).

I am not asking what XmlReader.Create() does, what I want to learn is why I would use static constructor over than ? What kind of aspect would it provide ? What are the things I can do with static constructor but I can't do with new keyword constructor.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原来分手还会想你 2024-08-16 13:33:51

首先,让我们按顺序了解术语。 XmlReader.Create 不是静态构造函数。它只是一个静态方法(通常)返回对象的新实例;这通常称为“工厂方法”。 “静态构造函数”是用关键字 static 声明的构造函数,用于初始化类的静态成员:

class MyClass {
   static MyClass() { ... } // static constructor
}

现在解释为什么工厂方法可能更可取。可能有几个原因。

其一,构造函数(通过 new 调用)总是必须提供新实例化的对象,或者抛出异常。如果有意义的话,工厂方法可以返回 null,或者它可以维护一些对象缓存,并避免一直创建新的对象(例如,当对象不可变时)。

另一个原因是,当您执行new T()时,您总是获得一个T的具体实例。工厂方法可以根据输入参数和其他因素创建 T 某些子类的实例。对于 XmlReader 来说,这正是发生的情况 - XmlReader 本身是 abstract,因此不可能有它的任何实例;但是,有几个子类具有不同的用途(验证/非验证、流后端/支持 DOM 等),并且 XmlReader.Create 根据您提供给它的重载和参数选择正确的子类。

First of all, let's get terminology in order. XmlReader.Create is not a static constructor. It's just a static method that (typically) returns new instances of objects; this is normally called "factory method". A "static constructor" would be a constructor declared with keyword static, used to initialize static members of the class:

class MyClass {
   static MyClass() { ... } // static constructor
}

Now as to why a factory method may be preferable. There can be several reasons.

For one, a constructor (invoked via new) always has to either provide a newly instantiated object, or throw an exception. A factory method can return null if that makes sense, or it may maintain some cache of objects, and avoid creating a new one all the time (e.g. when objects are immutable).

Another reason is that when you do new T(), you always get specifically an instance of T. A factory method could instead create an instance of some subclass of T, depending on input parameters and other factors. In case of XmlReader, this is precisely what happens - XmlReader itself is abstract, so there cannot be any instances of it; however, there are several subclasses that serve different purposes (validating/non-validating, stream backend / DOM backed, etc), and XmlReader.Create picks the right one based on overload and arguments you supply to it.

桜花祭 2024-08-16 13:33:51

静态构造函数用于初始化任何静态数据,或执行只需执行一次的特定操作。在创建第一个实例或引用任何静态成员之前自动调用它。经典构造函数用于初始化实例变量,并在每次创建对象时调用

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.Classic constructor is used to initialize instance variables, and called every time the object is created

清欢 2024-08-16 13:33:51

您可以在两个构造函数中执行相同的操作。没有什么区别。但是,静态构造函数在程序的生命周期中仅被调用一次,并且仅允许用于具有静态成员的类。它可以在使用私有静态变量之前对其进行初始化。

You can do the same in both constructors. THere is no difference. However, the static constructor is only called ONCE in the lifetime of your program and is only allowed for classes with static members. It can initialize the private static variables before using them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文