初始化程序和静态初始化程序之间的区别?
当我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,让我们按顺序了解术语。
XmlReader.Create
不是静态构造函数。它只是一个静态方法(通常)返回对象的新实例;这通常称为“工厂方法”。 “静态构造函数”是用关键字static
声明的构造函数,用于初始化类的静态成员:现在解释为什么工厂方法可能更可取。可能有几个原因。
其一,构造函数(通过
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 keywordstatic
, used to initialize static members of the class: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 returnnull
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 ofT
. A factory method could instead create an instance of some subclass ofT
, depending on input parameters and other factors. In case ofXmlReader
, this is precisely what happens -XmlReader
itself isabstract
, 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), andXmlReader.Create
picks the right one based on overload and arguments you supply to it.静态构造函数用于初始化任何静态数据,或执行只需执行一次的特定操作。在创建第一个实例或引用任何静态成员之前自动调用它。经典构造函数用于初始化实例变量,并在每次创建对象时调用
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
您可以在两个构造函数中执行相同的操作。没有什么区别。但是,静态构造函数在程序的生命周期中仅被调用一次,并且仅允许用于具有静态成员的类。它可以在使用私有静态变量之前对其进行初始化。
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.