为什么我们有静态课 [英] Why do we have static class

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

问题描述

大家好,

为什么我们在.Net框架中有静态类?
好像我们不创建staic类的对象?那么,如何说opps概念下的staic类又是紧随其后的呢?正如OOP所说的面向对象编程?

PS:-
我确实了解使用staic类具有以下优点
1)编译器确保不会偶然创建任何实例.
2)可以直接调用Static类的成员,而无需创建任何对象.
3)静态类包含静态成员
4)加载静态类时我们拥有静态类的名称空间或程序也被加载.

请帮帮我.

在此先谢谢您.

Hi All,

Why do we have static class in .Net framework?
As if we dont create object of staic classes? So how do we say that staic class is following under opps conceps? As OOP says Object Oriented programming?

PS:-
I do understand that using staic class have following advantages
1) Compiler ensures that no instance are incidently created.
2) Member of Static class can be called directly no need to create any object.
3) Static class contain static members
4) The namespace or program in which we have static class when that got loaded static class also got loaded.

Please help me.

Thanks in Advance.

推荐答案

拥有静态类的优点之一是提供可在整个应用程序范围内访问的方法.
例如. System.configuration dll中的ConfigurationManager类使我们可以跨应用程序读取App.Config文件.请参见MSDN信息. http://msdn.microsoft.com/en-us/library/system.configuration. configurationmanager.aspx [ ^ ]

通常,静态类应包含整个应用程序所需的信息,例如应用某些应用程序格式.可以通过创建实用程序类来定义此类应用程序范围的功能. (例如ConfigurationManager-用于访问配置文件的实用程序.)
One of the advantages of having static class is to provide its methods accessible application wide.
E.g. ConfigurationManager class in System.configuration dll allows us to read App.Config file across application. See MSDN info for it. http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx[^]

In general static class should contain information required across application such as applying certain application formatting. Such application wide functionalities can be defined by creating utility classes. (like ConfigurationManager - utility to access config file.)


静态类是一种使类与OOP没有共同点的方法.从本质上讲,这是一组成员,它们完全等效于一组静态变量,常量和方法(添加了井属性),因为它们在进行OOP之前就已被理解.可以通过使类的所有成员静态来获得此效果.另一个额外的功能是添加了访问修饰符.通过使用关键字 static 使类变为静态,使完全没有功能上的差异.

那么,为什么要在类中使用关键字 static ?

这只是支持编程活动的功能,一种用于防止意外编程​​错误的设备.如果这样做,它会阻止我们:

  • 在类中意外添加实例成员;
  • 在创建实例时意外实例化类.
  • 将类用作泛型类型参数,类型为约束 new.
A static class is a way to make a class which has nothing in common with OOP. Essentially, this is a package of members which are completely equivalent to a set of static variables, constants and methods (well properties are added) as they are understood before adventure of OOP. This effect can be obtained by having all members of the class static. Another extra feature is added access modifiers. Making class static by using key word static makes no functional difference at all.

So, why using key word static for a class?

This is just a feature of support of programming activity, a device used to protect us from accidental programming mistakes. If we do it, it prevents us from:

  • Accidentally adding an instance member to the class;
  • Accidental instantiation of the class when creating of the instance would not make sense.
  • Using a class as a generic type parameter with the type constraint new.


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

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