密封类和构造函数/解构器 [英] Sealed classes and constructors / deconstructors

查看:82
本文介绍了密封类和构造函数/解构器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




因为密封的课程不能用新的关键字实例化,例如CClass

objClass = new CClass(),这是否意味着他们没有构造函数/

解构器,或者,如果他们这样做,那么构造函数中的代码/

desconstructor永远不会运行?


Mark

Hi,

Since sealed classes can''t be instantiated with the new keyword e.g. CClass
objClass = new CClass(), does this mean that they don''t have constructors /
deconstructors or, if they do, that the code inside the constructor /
desconstructor will never run?

Mark

推荐答案

Mark Rae写道:
Mark Rae wrote:


因为密封的类不能用new关键字实例化,例如CClass
objClass = new CClass(),这是否意味着他们没有构造函数/
解构器,或者,如果他们这样做,构造函数/
析构函数中的代码将永远不会运行?


Hi,

Since sealed classes can''t be instantiated with the new keyword e.g. CClass
objClass = new CClass(), does this mean that they don''t have constructors /
deconstructors or, if they do, that the code inside the constructor /
desconstructor will never run?

Mark




嘿Mark,


实际密封的课程可以是实例化并在c#中有一个常规类

行为。 密封是指密封。 part意味着该类不能通过继承来扩展其他类。例如,如果您有这样的

类:


公共密封类MySealed

{

....

}


你不能这样做:


公共类SomeClass:MySealed

{

....

}



Hey Mark,

actually sealed classes can be instantiated and have a regular class
behavior in c#. The "sealed" part means that the class cannot be
extended by other classes by inheritence. For instance if you have a
class like this:

public sealed class MySealed
{
....
}

you cannot do this:

public class SomeClass : MySealed
{
....
}


Mark,


你错了。密封类意味着它们无法从中获得

。这并不意味着你不能创造它们。相反,静态类

不能从自己创建或拥有自己的实例。


如果你不能实例化一个类(无论出于何种原因) ),然后

构造函数和终结器永远不会被调用。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse .com


" Mark Rae" <毫安** @ mark-N-O-S-P-A-M-rae.co.uk>写在消息

新闻:美国************** @ TK2MSFTNGP12.phx.gbl ...
Mark,

You are mistaken. Sealed classes mean that they can not be derived
from. It doesn''t mean that you can''t create them. Rather, static classes
can not be derived from or have instances of themselves created.

And if you can''t instantiate a class (for whatever reason), then the
constructor and the finalizer is never called.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:uS**************@TK2MSFTNGP12.phx.gbl...

由于密封的类不能用new关键字实例化,例如
CClass objClass = new CClass(),这是否意味着他们没有
构造函数/解构函数,或者如果他们这样做,
构造函数/ desconstructor中的代码将永远不会运行?

Mark
Hi,

Since sealed classes can''t be instantiated with the new keyword e.g.
CClass objClass = new CClass(), does this mean that they don''t have
constructors / deconstructors or, if they do, that the code inside the
constructor / desconstructor will never run?

Mark



" laimis" < SI ***** @ iit.edu>在消息中写道

新闻:呃************** @ TK2MSFTNGP14.phx.gbl ......
"laimis" <si*****@iit.edu> wrote in message
news:er**************@TK2MSFTNGP14.phx.gbl...
密封类可以实例化,并在c#中有常规的类行为。 密封是指密封。部分意味着该类不能通过继承扩展到其他类。例如,如果你有一个像
这样的课程:
actually sealed classes can be instantiated and have a regular class
behavior in c#. The "sealed" part means that the class cannot be extended
by other classes by inheritence. For instance if you have a class like
this:




是的,抱歉 - 我对密封和静电感到困惑。

我想问的是,如果一个密封的类只提供静态方法

,这些方法在没有课堂启发的情况下被引用,那是否意味着

甚至如果那个类有一个构造函数,那么构造函数中的代码将会是什么?b $ b永远不会开火?



Yes, sorry - I got myself confused with sealed and static.

What I meant to ask was that if a sealed class provides only static methods
which are referenced without instiation of the class, does that mean that
even if that class has a constructor, the code inside the constructor will
never fire?


这篇关于密封类和构造函数/解构器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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