非静态类中的静态方法和静态类中的静态方法有什么区别?

发布于 2024-10-21 06:00:35 字数 416 浏览 2 评论 0原文

我有两个类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逆光下的微笑 2024-10-28 06:00:35

唯一的区别是非静态类中的静态方法不能是扩展方法


换句话说,这是无效的:

class Test
{
    static void getCount(this ICollection<int> collection)
    { return collection.Count; }
}

而这是有效的:

static class Test
{
    static void getCount(this ICollection<int> collection)
    { return collection.Count; }
}

The only difference is that static methods in a nonstatic class cannot be extension methods.


In other words, this is invalid:

class Test
{
    static void getCount(this ICollection<int> collection)
    { return collection.Count; }
}

whereas this is valid:

static class Test
{
    static void getCount(this ICollection<int> collection)
    { return collection.Count; }
}
烟凡古楼 2024-10-28 06:00:35

静态类只能包含静态成员。

静态方法可以确保,即使您要创建多个 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.

若相惜即相离 2024-10-28 06:00:35

如果您有一个仅包含静态方法的非静态类,则可以创建该类的实例。但您无法有意义地使用该实例。注意:当您没有定义构造函数时,编译器会为您添加一个。

静态类没有构造函数,因此无法创建它的实例。当您向其中添加实例方法(您指的是静态方法)时,编译器也会给出错误。

另请参阅 文档

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.

折戟 2024-10-28 06:00:35

静态方法属于类,非静态方法属于类的对象。也就是说,非静态方法只能在其所属类的对象上调用。静态方法只能访问静态成员。非静态方法可以访问静态和非静态成员,因为在调用静态方法时,该类可能不会被实例化(如果在类本身上调用它)。在另一种情况下,只有当类已经实例化时才能调用非静态方法。静态方法由类的所有实例共享。每当在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文