什么是“静态方法”?在 C# 中?
将 static 关键字添加到方法中意味着什么?
public static void doSomething(){
//Well, do something!
}
您可以将 static
关键字添加到类中吗?那意味着什么?
What does it mean when you add the static keyword to a method?
public static void doSomething(){
//Well, do something!
}
Can you add the static
keyword to class? What would it mean then?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
与常规(实例)函数不同,
静态
函数不与类的实例关联。static
类是只能包含static
成员的类,因此无法实例化。例如:
为了调用 InstanceMethod,您需要该类的实例:
A
static
function, unlike a regular (instance) function, is not associated with an instance of the class.A
static
class is a class which can only containstatic
members, and therefore cannot be instantiated.For example:
In order to call
InstanceMethod
, you need an instance of the class:从另一个角度来看:
考虑一下您想要对单个字符串进行一些更改。
例如你想把字母变成大写等等。
您为这些操作创建另一个名为“Tools”的类。
创建“Tools”类的实例是没有意义的,因为该类中没有任何类型的可用实体(与“Person”或“Teacher”类相比)。
所以
我们使用 static 关键字是为了在不创建任何实例的情况下使用“Tools”类,当您在类名(“Tools”)后面按点时,您可以访问您想要的方法。
From another point of view:
Consider that you want to make some changes on a single String.
for example you want to make the letters Uppercase and so on.
you make another class named "Tools" for these actions.
there is no meaning of making instance of "Tools" class because there is not any kind of entity available inside that class (compare to "Person" or "Teacher" class).
So
we use static keyword in order to use "Tools" class without making any instance of that, and when you press dot after class name ("Tools") you can have access to the methods you want.
即使尚未创建类的实例,也可以在类上调用静态方法、字段、属性或事件。如果创建了该类的任何实例,则它们不能用于访问静态成员。静态字段和事件只存在一份副本,静态方法和属性只能访问静态字段和静态事件。静态成员通常用于表示不随对象状态而改变的数据或计算;例如,数学库可能包含用于计算正弦和余弦的静态方法。
静态类成员在成员的返回类型之前使用 static 关键字声明
A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for instance, a math library might contain static methods for calculating sine and cosine.
Static class members are declared using the static keyword before the return type of the member
静态函数意味着它与类相关联(不是类的特定实例,而是类本身),即使不存在类实例也可以调用它。
静态类意味着该类仅包含静态成员。
Static function means that it is associated with class (not a particular instance of class but the class itself) and it can be invoked even when no class instances exist.
Static class means that class contains only static members.
很快您就无法实例化静态类:
例如:
你不能这样制作:
你只能制作:
Shortly you can not instantiate the static class:
Ex:
You can not make like this:
You can make only:
当您向方法添加“static”关键字时,这意味着底层实现为该类的任何实例提供相同的结果。
不用说,结果会随着参数值的变化而变化
When you add a "static" keyword to a method, it means that underlying implementation gives the same result for any instance of the class.
Needless to say, result varies with change in the value of parameters
static 关键字的核心,您在 RAM 中只会拥有该关键字的一份副本(方法 /variable /class )
所有通话共享
Core of the static keyword that you will have only one copy at RAM of this (method /variable /class )
that's shared for all calling
静态变量不与类的对象链接。可以使用类名来访问它。类的所有对象将共享静态变量。
通过将函数设置为静态,它将限制该文件内该函数的访问。
Static variable doesn't link with object of the class. It can be accessed using classname. All object of the class will share static variable.
By making function as static, It will restrict the access of that function within that file.
static 关键字应用于类时,告诉编译器创建该类的单个实例。这样就不可能“新建”该类的一个或多个实例。静态类中的所有方法本身都必须声明为静态。
拥有非静态类的静态方法是可能的,而且通常是可取的。例如,在创建另一个类的实例时,工厂方法通常被声明为静态,因为这意味着不需要包含因子方法的类的特定实例。
有关方式、时间和地点的详细说明,请参阅
The static keyword, when applied to a class, tells the compiler to create a single instance of that class. It is not then possible to 'new' one or more instance of the class. All methods in a static class must themselves be declared static.
It is possible, And often desirable, to have static methods of a non-static class. For example a factory method when creates an instance of another class is often declared static as this means that a particular instance of the class containing the factor method is not required.
For a good explanation of how, when and where see MSDN