公共静态和公共变量 [英] public static and public variabled

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

问题描述

尊敬的先生;

例如,c#中的公共静态变量和公共变量之间有什么区别?

在此先感谢
Dalia

Dear sir;

What is the difference between public static and public variables in c# by example?

Thanks in advance
Dalia

推荐答案

让我说我有一个课
lets say I have a class
class A
{
 public int x;
 static int y;
}


现在的区别:
要使用公共变量,我必须创建对象,这样


now the differences:
to use public variable i have to have objects created so

A a = new A();
int num = a.x;


另一方面,静态变量处于类级别,因此我可以用作


on the other hand static variables are at class level so I can be used as

int num = A.y;




or

A a = new A();
int num = a.y;


同样,静态变量将保留该类所有对象的值.为了说明,让我们看一下:


ALso the static variable retain the values for all objects of the class. to illustrate lets see this:

A a1 = new A();
A a2 = new A();

a1.x = 3;
a2.x = 5;

Console.Write(a1.x);    //print 3
Console.Write(a2.x);    //print 5

a1.y = 3;
Console.Write(a1.y);    //print 3
Console.Write(a2.y);    //print 3

a2.y = 5;
Console.Write(a1.y);    //print 5
Console.Write(a2.y);    //print 5


最后,我们永远不要使用公共成员变量,而应该始终使用封装在属性或方法内部的私有变量.另一方面,类中可能需要静态变量.

注意:此处的代码仅供演示之用,可能包含一些错误,因为我尚未对其进行编译.同样,这也不是完整的解释.这只是基本的解释.我建议阅读有关此内容的更多信息,以全面理解这些内容.


On the closing note, we should never use public member variables, we should always use private variables encapsulated inside properties or methods. on the other hand static variables might be required in a class.

Note: code here is for demonstration purpose, might contain come errors as I have not compiled it. ALso this is not the complete explanation. it is just the basic explanation. i suggest reading more about this to understand these things fully.


此处: C#主题:静态变量 [ ^ ]


很难弄清您要学习的内容,因为它们不是一回事-还是不建议使用其中的至少一个.

从头开始:
变量有两种类型:实例和静态.如果未将变量声明为static,则该变量始终是实例.区别在于,实例变量是类的特定实例的一部分,而static变量是该类的所有实例所共有的,并且不需要生成实例即可进行访问.

例如,乘汽车-真正的汽车.
汽车有多少个轮子? 4-否则就不会是汽车.因此,WheelsCount将是所有汽车的静态属性-您可以询问Car.WheelsCount并希望它每次都能工作.
汽车是什么颜色的?不是-汽车有多种颜色,因此您必须具体说明正在谈论的是哪辆汽车-或您想知道哪种颜色的汽车:您的汽车是什么颜色?" 我的车是什么颜色?" 那辆车是什么颜色?".颜色是Car的实例属性.

静态/实例差异适用于C#中的变量,属性和方法
Publicstatic/instance的区别没有任何关系-它仅定义可以访问该项目的类:private项目只能由其包含在其中的类访问,protected该类及其派生类,internal来自同一程序集中的所有类,public任何位置的任何类.
如果再回到汽车类比,则private汽车将是只有钥匙的地方,protected你和妻子有钥匙的地方,而public是将汽车停在房子外然后离开点火钥匙!

您可以具有公共静态变量或私有静态变量-这两个修饰符不相关.


固定到内部-从名称空间到程序集已更正.感谢VJ Reddy! -OriginalGriff [/edit]
It''s difficult to work out what you are trying to learn, because they aren''t the same thing - and at least one of them is not recommended anyway.

Starting at the beginning:
There are two types of variable: instance and static. If you do not declare a variable as static, then it is always instance. The difference is that an instance variable is a part of a particular instance of a class while a static variable is common to all instances of the class, and does not require a instance be generated in order to be accessed.

For example, take cars - real ones.
How many wheels has a car? 4 - or it wouldn''t be a car. So WheelsCount would be a static property of all cars - you can ask Car.WheelsCount and expect it to work every time.
What colour is a car? It isn''t - cars come in a variety of colours, so you have to pecific which car you are talking about - or which instance of a Car you want to know the colour of: "What colour is your car?" "What colour is my car?" "What colour is that car?". Colour is an instance property of a Car.

This static / instance difference is applicable to variables, properties and methods in C#

Public does not have any relation to the static / instance difference - it just defines what classes can access the item: a private item can only be accessed by the class it is contained within, protected by that class and those derived from it, internal from all classes in the same assembly, and public by any class anywhere.
If we go back to the cars analogy, a private car would be one where only you have the key, protected where you and your wife have keys, and public would be when you park it outside the house and leave the keys in the ignition!

You can have public static variables, or private static variables - the two modifiers are not related.


[edit]Fix to internal - corrected from namespace to assembly. Thanks VJ Reddy! - OriginalGriff[/edit]


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

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