静态构造函数和私有构造函数有什么区别? [英] What is the difference between static constructor and private constructor?

查看:95
本文介绍了静态构造函数和私有构造函数有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问私有构造函数类,即使它没有被继承也没有实例化。



如果我们在私有构造函数类中使用静态方法并由其类访问名称。然后静态构造函数表示什么?



如果在私有构造函数类中没有静态方法,我们可以在类中访问成员

。 br />


我尝试了什么:



我有点困惑当我以前面对面试时。

How to access the private constructor class even it is not inherited nor instantiated.

If we are having static methods in the private constructor class and accessed by its class name. Then what the static constructor signifies?

Is it possible without static method in private constructor class can we access members
in the class.

What I have tried:

I have a little confusion in that when I used to face the interview.

推荐答案

请查看下面的网址。这将对你有所帮助



C Sharp中的构造函数 [ ^ ]
Please check below url. this will help you

Constructor in C Sharp[^]


几乎所有有什么不同的问题,这个都是完全错误的。这些概念并不直接相关。 Private是关于可访问性的,静态vs实例是每个类vs每个实例。刚看完构造函数,你会看到。



由于静态非私有构造函数完全没有意义,语法禁止静态构造函数是非私有的,但是这是唯一的原因,它没有任何基础。



所以,学习两个独立的东西:访问说明符及其含义(私有,受保护,内部,内部受保护静态意味着什么。



对于方法,静态表示没有将this传递给所有实例方法的隐式参数。即使对于构造函数也是如此:与C ++相比,.NET构造函数使用几乎构造的对象,并且可以通过this(显式或隐式使用)以及静态成员在其实现中操作任何实例成员。



当然,静态构造函数无权访问任何实例成员,因为还没有实例(还)。它甚至可以在应用程序入口点方法( Main )之前工作 - 这是一个流行的棘手问题。这是一个非常有趣的作品。准备工作需要静态构造函数的代码,它与类本身有关,而不是任何类实例。例如,它用于初始化一些实例数据成员。因此,运行时系统应该保证在创建类的第一个实例之前调用这样的构造函数。



有趣的是,静态构造函数不相关是不正确的完全类实例,因为如果在运行时根本没有使用它,则不会调用它。任何实例成员的静态成员的使用(如果不构造至少一个实例,则不能使用实例成员)在使用之前触发静态构造函数的调用。当然,如果静态构造函数在基类中,派生类也是如此。



如果必须使用静态构造函数,理解所有这些背景非常重要使用。



-SA
As almost all "what is the difference" question, this one is totally incorrect. Those notions are not directly related. Private is about accessibility, static vs instance is per-class vs per instance. Just read about constructors and you will see.

As static non-private constructor would make no sense at all, the syntax prohibit static constructors to be non-private, but this is the only reason, there is nothing fundamental in it.

So, learn two separate thing: access specifiers and their meaning (private, protected, internal, internal protected and public) and what does "static" means.

For methods, "static" means having no implicit parameter "this" passed to all instance methods. This is true even for constructors: in contrast to C++, .NET constructors work with "almost constructed" object and can operate any instance members in its implementation through "this" (explicitly or implicitly used), and also static members.

Naturally, a static constructor does not have access to any instance members, because there is no instance (yet). It can work even before application entry point method (Main) — this is a popular tricky question. This is a very interesting piece in general. The code of static constructor is needed for preparation work which is related to the class itself, not any of the class instances. For example, it's used to initialize some instance data members. Therefore, the runtime system should guarantee that such constructor is called before the very first instance of the class is created.

Interestingly, it's no true that static constructors are not related to class instances at all, because it won't be called if the class is not used during runtime at all. Any use of any static members of instance members (a use of instance members cannot happen without constructing at least one instance) trigger the call of a static constructor before the use. Naturally, this is also true for derived classes, if the static constructor is in a base class.

It's very important to understand all this background if static constructors have to be used.

—SA


这篇关于静态构造函数和私有构造函数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆