非静态类中的静态方法和静态类中的静态方法有什么区别?
我有两个类 Class A 和 ClassB:
static class ClassA
{
static string SomeMethod()
{
return "I am a Static Method";
}
}
class ClassB
{
static string SomeMethod()
{
return "I am a Static Method";
}
}
我想知道 ClassA.SomeMethod();
和 ClassB.SomeMethod();
之间有什么区别,
当它们都可以时无需创建类的实例即可访问,为什么我们需要创建静态类,而不是仅使用非静态类并将方法声明为静态?
I have two classes Class A and ClassB:
static class ClassA
{
static string SomeMethod()
{
return "I am a Static Method";
}
}
class ClassB
{
static string SomeMethod()
{
return "I am a Static Method";
}
}
I want to know what is the difference between ClassA.SomeMethod();
and ClassB.SomeMethod();
When they both can be accessed without creating an instance of the class, why do we need to create a static class instead of just using a non static class and declaring the methods as static?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
唯一的区别是非静态类中的静态方法不能是扩展方法。
换句话说,这是无效的:
而这是有效的:
The only difference is that static methods in a nonstatic class cannot be extension methods.
In other words, this is invalid:
whereas this is valid:
静态类只能包含静态成员。
静态方法可以确保,即使您要创建多个 classB 对象,它们也只会使用单个共享的 SomeMethod 函数。
从技术上讲,没有什么区别,除了 ClassA 的 SomeMethod 必须 是静态的。
A static class can only contain static members.
A static method ensures that, even if you were to create multiple classB objects, they would only utilize a single, shared SomeMethod function.
Technically, there's no difference, except that ClassA's SomeMethod must be static.
如果您有一个仅包含静态方法的非静态类,则可以创建该类的实例。但您无法有意义地使用该实例。注意:当您没有定义构造函数时,编译器会为您添加一个。
静态类没有构造函数,因此无法创建它的实例。当您向其中添加实例方法(您指的是静态方法)时,编译器也会给出错误。
另请参阅 文档。
If you have a non-static class containing only static methods, you could create an instance of that class. But you can't use that instance meaningfully. NB: when you don't define a constructor, the compiler adds one for you.
A static class does not have a constructor, so you can't create an instance of it. Also the compiler gives an error when you add an instance method to it (where you meant a static method).
See also docs.
静态方法属于类,非静态方法属于类的对象。也就是说,非静态方法只能在其所属类的对象上调用。静态方法只能访问静态成员。非静态方法可以访问静态和非静态成员,因为在调用静态方法时,该类可能不会被实例化(如果在类本身上调用它)。在另一种情况下,只有当类已经实例化时才能调用非静态方法。静态方法由类的所有实例共享。每当在 C++/Java/C# 中调用方法时,都会与/不与其他参数一起传递隐式参数(“this”引用)。在静态方法调用的情况下,不会传递“this”引用,因为静态方法属于类,因此没有“this”引用。
A static method belongs to the class and a non-static method belongs to an object of a class. That is, a non-static method can only be called on an object of a class that it belongs to. A static method can access only static members. A non-static method can access both static and non-static members because at the time when the static method is called, the class might not be instantiated (if it is called on the class itself). In the other case, a non-static method can only be called when the class has already been instantiated. A static method is shared by all instances of the class. Whenever a method is called in C++/Java/C#, an implicit argument (the ‘this’ reference) is passed along with/without the other parameters. In case of a static method call, the ‘this’ reference is not passed as static methods belong to a class and hence do not have the ‘this’ reference.