我们可以在静态类中使用静态成员变量吗? [英] Can we have static member variable in static class?

查看:95
本文介绍了我们可以在静态类中使用静态成员变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们将任何类声明为静态类,并将该类中的成员变量声明为static。究竟会发生什么?这样做好吗?



If we are declaring any class as static class and also member variable in that class as static. What will exactly happen? Is it good to do so?

Public static class DataManager
{
 public static List<int> productIDs=new List<int>();
// Some code....
//
}

推荐答案

是的,你的课程是正确的你想知道什么。



对于静态类,它不可能创建一个对象。在静态类中只允许静态成员,这意味着在静态类中不可能编写非静态方法。



检查我的文章与静态类和方法

基本C#OOP概念 [ ^ ]
Yes your class is correct what exactly you want to know.

For Static Class it’s not possible to create an Object. In Static Class only Static members are allowed which means in a static Class it’s not possible to write non-static method.

Check my article related to Static Class and Method
Basic C# OOP Concept[^]


"Is it good to do so?"

imho,当你做出一个有意识的选择时它是最好的 e使用静态实现方法,因为它是你的代码所做的,它的使用方式,你的意图和应用程序设计的有机结果。



它如果你每次使用'静态关键字时问自己这个问题可能会有所帮助:



我打算/需要/想要这个(无论如何)成为这个的唯一一个?取决于y我们的文化,你也可能会想到(典型的西方)婚姻誓言的那部分,即放弃所有其他人......直到死亡让我们分开。:)



请记住,可以用'new实例化的公共类可以有静态变量,方法。实际上.NET本身就是这样的例子:即'String对象。



'static in .NET的重要用途包括:



1.构建实用程序类,提供静态方法库(或扩展方法)。



2.创建'单身课程



3.实施'工厂模式



C#中的'静态访问修饰符是滥用,imho,当它被用作创建等效全局变量的方法时(如JavaScript和Visual Basic中所见),以避免在对象/类之间传递对象/数据/状态的更正式技术/ controls。

imho, it is "best" when you have made a conscious choice to use a static implementation method because it is an "organic" consequence of what your code does, how it is used, and your intent, and application design.

It may be helpful if you ask yourself this question every time you use the 'static keyword:

"Do I intend/need/want for this (whatever) to be the one-and-only-one of this ? Depending on your culture, you might also think of that part of the (typical "western") marriage vows that says "forsaking all others ... until death do us part." :)

Keep in mind that public classes that can be instantiated with 'new can have static variables, methods. In fact .NET itself is full of examples of this: i.e., the 'String object.

Among important uses of 'static in .NET are:

1. constructing utility classes that provide libraries of static methods (or extension methods).

2. in creating 'singleton Classes

3. in implementing the 'Factory Pattern

The 'static access modifier in C# is "abused," imho, when it is used as a way to create the equivalent of global variables (as found in JavaScript, and Visual Basic) in order to avoid more "formal" techniques for passing objects/data/state between objects/classes/controls.


静态类中的静态成员变量是可能的......



我们无法创建静态类的实例,

例如。





公共静态类样本



{

public static fun_sample()



{



}

}



我们可以访问这个方法直接通过sample.fun_sample();
static members variable in static class is possible...

we can't create instance of static class ,
eg.


public static class sample

{
public static fun_sample()

{

}
}

we can access this method directly by sample.fun_sample();


这篇关于我们可以在静态类中使用静态成员变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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