C# 非静态类中的静态方法有什么意义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1179808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the point of a static method in a non-static class?
提问by Matt
I have trouble understanding the underlying errors with the code below:
我无法理解以下代码的潜在错误:
class myClass
{
public void print(string mess)
{
Console.WriteLine(mess);
}
}
class myOtherClass
{
public static void print(string mess)
{
Console.WriteLine(mess);
}
}
public static class Test
{
public static void Main()
{
myClass mc = new myClass();
mc.print("hello");
myOtherClass moc = new myOtherClass();
moc.print("vhhhat?");
//This says I can't access static method in non static context, but am I not?
}
}
I can't ever think of a reason why one would declare a static method in a non-static class, so why will .NET not throw an exception error.
我想不出为什么要在非静态类中声明静态方法的原因,那么为什么 .NET 不会抛出异常错误。
Furthermore,
此外,
moc.print("vhhhat?");
This will say I can't access static method in non static context but Test and main are static, what is it referring to ?
这会说我无法在非静态上下文中访问静态方法,但 Test 和 main 是静态的,它指的是什么?
采纳答案by AlbertoPL
A static class means that you cannot use it in a non-static context, meaning that you cannot have an object instantiation of that class and call the method. If you wanted to use your print method you would have to do:
静态类意味着您不能在非静态上下文中使用它,这意味着您不能拥有该类的对象实例并调用该方法。如果您想使用打印方法,则必须执行以下操作:
myOtherClass.print("vhhhat?");
This is not static, as you created an instantiation of the class called moc
:
这不是静态的,因为您创建了一个名为 的类的实例化moc
:
myOtherClass moc = new myOtherClass();
回答by EFraim
When you are calling a method on an object instanceyou are calling it in a non-static context. It is of no importance in which method you do this.
当您在对象实例上调用方法时,您是在非静态上下文中调用它。使用哪种方法执行此操作并不重要。
回答by Reed Copsey
This will say I can't access static method in non static context but Test and main are static, what is it referring to ?
这会说我无法在非静态上下文中访问静态方法,但 Test 和 main 是静态的,它指的是什么?
This is referring to the fact that you're referencing a static method (myOtherClass.print) using an instance (moc). You would have to rework this to be:
这是指您使用实例 (moc) 引用静态方法 (myOtherClass.print) 的事实。您必须将其修改为:
myOtherClass.print("vhhhat?");
That will compile correctly.
这将正确编译。
Static methods are methods that work on the class itself, not a specific instance of the class. This has many uses - one example is for implementing the Factory method pattern.
静态方法是作用于类本身的方法,而不是类的特定实例。这有很多用途——一个例子是实现工厂方法模式。
回答by ars
Sometime the "objective" of the function is particular to the class rather than the object (instance of the class).
有时,函数的“目标”特定于类而不是对象(类的实例)。
For example, a factory method:
例如,工厂方法:
SomeClass obj = SomeClass.CreateInstance();
This is more apparent when the language has metaprogramming facilities that allow operations on classes. In Python, this distinction is made more explicit by convention: the first parameter passed to a function is either named something like "cls" or "self" and indicates that the function might operate on the class (when it's a "class method") or the instance (the type you're more used to, when it's an instance method).
当语言具有允许对类进行操作的元编程设施时,这一点更加明显。在 Python 中,按照惯例,这种区别更加明确:传递给函数的第一个参数要么命名为“cls”或“self”,并表示该函数可能对类进行操作(当它是“类方法”时)或实例(您更习惯的类型,当它是实例方法时)。
回答by Sean
First, the error:
首先,错误:
moc.print("vhhhat?");
Is trying to call a static method on an instance of the class (i.e. a non-static context). To properly call print(), you would do
正在尝试对类的实例(即非静态上下文)调用静态方法。要正确调用print(),你会这样做
myOtherClass.print("vhhhat?");
For the first question, there are many reasons to have static methods in a non-static class. Basically, if there is an operation associated with the class, but not with any particular instance of the class, it should be a static method. For example, String.Format() (or any of the String static methods) should not operate on string instances, but they should be associated with the String class. Therefore they are static.
对于第一个问题,在非静态类中有静态方法的原因有很多。基本上,如果有一个与该类相关的操作,但不与该类的任何特定实例相关联,则它应该是一个静态方法。例如,String.Format()(或任何 String 静态方法)不应对字符串实例进行操作,但它们应与 String 类相关联。因此它们是静态的。
回答by Adam Prescott
Here's a good example of when you would use static methods in a non-static class:
下面是在非静态类中使用静态方法的一个很好的例子:
回答by Shruti Sehgal
The correct program would be:-
正确的程序是:-
class myClass
{
public void print(string mess)
{
Console.WriteLine(mess);
}
}
class myOtherClass
{
public static void print(string mess)
{
Console.WriteLine(mess);
}
public void printMe(string mess)
{
Console.WriteLine(mess);
}
}
public static class Test
{
public static void Main()
{
myClass mc = new myClass();
mc.print("hello");
myOtherClass moc = new myOtherClass();
myOtherClass.print("vhhhat?");
moc.printMe("test me");
}
}