关于“静态类”的问题 [英] Question about "static class"

查看:89
本文介绍了关于“静态类”的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力理解将一个类定义为''静态''对我来说是什么。


这是一个例子,因为也许我在想这一切都错了:


我的应用程序将允许用户控制应用程序使用的字体。

所以我需要根据用户

输入的设置更改字体。但是,对于我来说,使用
注册表,获取字体信息并为我要显示的每个表单创建新的字体对象似乎都是浪费。所以我想也许

''static''关键字以某种方式保住了我的救赎。我想要一个

clas从注册表中读取,创建字体,然后他们

是否有任何表格可供参考。 />

对此的任何评论都将不胜感激。


谢谢,

-Neil K.

I''m trying to understand what defining a class as ''static'' does for
me.

Here''s an example, because maybe I am thinking about this all wrong:

My app will allows the user to control the fonts that the app uses.
So I will need to change the fonts depending on what settings the user
has entered. However, it seems kind of wasteful to me to go to teh
registry, fetch the font information and create new font objects for
every form that I am going to display. So I was thinking that perhaps
the ''static'' keyword held my salvation somehow. I''d like to have a
clas that reads from the registry, creates the fonts, and then they
are there for any of the forms to reference.

Any comments on that would be greatly appreciated.

Thanks,
-Neil K.

推荐答案

虽然类可以有一个静态的构造函数和/或静态成员,但它没有静态类这样的东西 - 而且它如果你选择的话可以有那些,没有别的




静态成员可以在不需要创建

的情况下使用
如果你创建一个带有静态构造函数的类来获取你的注册表

info并将信息存储在静态成员中,那么是的,你的所有表单可以

通过引用类的名称访问此信息但不实例化

it:


--Bob

" Neil Kiser" < neil@NO_SPAM_PLEASEkiserville.com>在消息中写道

新闻:8v ******************************** @ 4ax.com ...
There is no such thing as a static class, although class can have a static
constructor and/or static members -- and it can have those and nothing else
if you choose.

Static members are available without having to create in instance of the
class.

If you create a class with a static constructor that fetches your registry
info and stores the info in static members, then yes, all your forms can
access this info by referencing the name of the class but not instantiating
it:

--Bob
"Neil Kiser" <neil@NO_SPAM_PLEASEkiserville.com> wrote in message
news:8v********************************@4ax.com...
我正在努力理解将一个类定义为静态对我来说是什么。

这是一个例子,因为也许我在考虑这一切都错了:

我的应用程序将允许用户控制应用程序使用的字体。
所以我需要根据设置更改字体用户
已进入。但是,对于我来说,我需要显示的每个表单都可以获取字体信息并创建新的字体对象,这对我来说似乎有点浪费。所以我在想,也许
''static''关键字以某种方式得到了我的救赎。我想要一个
clas从注册表中读取,创建字体,然后他们就可以参考任何表格。

任何评论非常感谢。

谢谢,
-Neil K。
I''m trying to understand what defining a class as ''static'' does for
me.

Here''s an example, because maybe I am thinking about this all wrong:

My app will allows the user to control the fonts that the app uses.
So I will need to change the fonts depending on what settings the user
has entered. However, it seems kind of wasteful to me to go to teh
registry, fetch the font information and create new font objects for
every form that I am going to display. So I was thinking that perhaps
the ''static'' keyword held my salvation somehow. I''d like to have a
clas that reads from the registry, creates the fonts, and then they
are there for any of the forms to reference.

Any comments on that would be greatly appreciated.

Thanks,
-Neil K.





" Bob Grommes" < bo*@bobgrommes.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP15.phx.gbl ...

"Bob Grommes" <bo*@bobgrommes.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
有没有静态类这样的东西,虽然类可以有一个静态的构造函数和/或静态成员 - 如果你选择的话,它可以有其他的东西
There is no such thing as a static class, although class can have a static
constructor and/or static members -- and it can have those and nothing
else if you choose.




从技术上来说,如果OP使用C#2(因为快递产品

beta出现了,可能性就在那里)。 C#2中的静态类只是

类,它们只能拥有静态成员并且可以自动化密封

模式,你可以用于纯静态类。


然而,在这个边缘情况下,答案的其余部分是完全有效的,那就是为了避免混淆(并且可能会创建一些,;)。

只是一种技术性问题。 />



Technically there is, if the OP was using C# 2(since the express product
betas came out, the possiblity is there). Static classes in C# 2 are simply
classes that can only have static members and that automate the sealing
pattern you ahve to use for a pure static class.

However, in that edge case, the rest of the answer is perfectly valid, thats
just a technicality to avoid confusion(And probably create some, ;).


海报可能正在谈论Visual Studio 2005(Whidbey),它介绍了静态类的概念 - 一个只能拥有静态成员且无法实例化的概念。


问候


Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft。 com / microsoft.public.dotnet.languages.csharp /< #X ************** @ TK2MSFTNGP15.phx.gbl>


那里虽然类可以有一个统计数据,但它不是静态类ic

构造函数和/或静态成员 - 它可以包含那些而不是其他

如果你选择的话。


静态成员可以在不需要创建

类的情况下使用。


如果你创建一个带有静态构造函数的类来获取你的注册表

info并将信息存储在静态成员中,然后是,所有表单都可以

通过引用类的名称访问此信息但不实例化

it :


--BOB

" Neil Kiser" < neil@NO_SPAM_PLEASEkiserville.com>在消息中写道

新闻:8v ******************************** @ 4ax.com ...
The poster may be talking about Visual Studio 2005 (Whidbey) which introduces the concept of a static class - one that can only have static members and cannot be instantiated.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#X**************@TK2MSFTNGP15.phx.gbl>

There is no such thing as a static class, although class can have a static
constructor and/or static members -- and it can have those and nothing else
if you choose.

Static members are available without having to create in instance of the
class.

If you create a class with a static constructor that fetches your registry
info and stores the info in static members, then yes, all your forms can
access this info by referencing the name of the class but not instantiating
it:

--Bob
"Neil Kiser" <neil@NO_SPAM_PLEASEkiserville.com> wrote in message
news:8v********************************@4ax.com...
我正在努力理解将一个类定义为静态对我来说是什么。

这是一个例子,因为也许我在考虑这一切都错了:

我的应用程序将允许用户控制应用程序使用的字体。
所以我需要根据设置更改字体用户
已进入。但是,对于我来说,我需要显示的每个表单都可以获取字体信息并创建新的字体对象,这对我来说似乎有点浪费。所以我在想,也许
''static''关键字以某种方式得到了我的救赎。我想要一个
clas从注册表中读取,创建字体,然后他们就可以参考任何表格。

任何评论非常感谢。

谢谢,
-Neil K。
I''m trying to understand what defining a class as ''static'' does for
me.

Here''s an example, because maybe I am thinking about this all wrong:

My app will allows the user to control the fonts that the app uses.
So I will need to change the fonts depending on what settings the user
has entered. However, it seems kind of wasteful to me to go to teh
registry, fetch the font information and create new font objects for
every form that I am going to display. So I was thinking that perhaps
the ''static'' keyword held my salvation somehow. I''d like to have a
clas that reads from the registry, creates the fonts, and then they
are there for any of the forms to reference.

Any comments on that would be greatly appreciated.

Thanks,
-Neil K.




---

接收邮件已通过无病毒认证。

由AVG反病毒系统检查( http://www.grisoft.com)

版本:6.0.771 /病毒库:518 - 发布日期:28/09/2004


[microsoft.public.dotnet.languages.csharp]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004

[microsoft.public.dotnet.languages.csharp]


这篇关于关于“静态类”的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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