TOC

This article is currently in the process of being translated into Chinese (~4% done).

类 :

Static members

从前面的章节中我们知道,和一个类打交道的常用方式是先实例化, 然后使用实例化后的对象. 大多数情况下, 类都是这样工作的; 我们可以把同一个类实例化很多次, 然后在不同的情况下使用它们. 然而, 有些情况下, 你可能需要使用一个类但是却并不想实例化它, 或者至少是使用一个类的成员而不创建对象. 比如, 你可能有一个类, 它的某个变量无论在任何情况下都保持不变. 这个变量就叫做静态成员, 因为它一直保持不变.

A class can be static, and it can have static members, both functions and fields. A static class can't be instantiated, so in other words, it will work more as a grouping of related members than an actual class. You may choose to create a non-static class instead, but let it have certain static members. A non-static class can still be instantiated and used like a regular class, but you can't use a static member on an object of the class. A static class may only contain static members.

First, here is an example of a static class:

public static class Rectangle
{
    public static int CalculateArea(int width, int height)
    {
        return width * height;
    }
}

As you can see, we use the static keyword to mark the class as static, and then we use it again to mark the method, CalculateArea, as static as well. If we didn't do that, the compiler would complain, since we can't have a non-static member of a static class.

To use this method, we call it directly on the class, like this:

Console.WriteLine("The area is: " + Rectangle.CalculateArea(5, 4));

We could add other helpful methods to the Rectangle class, but perhaps you are wondering why we are passing on width and height to the actual method, instead of storing it inside the class and then pulling them from there when needed? Because it's static! We could store them, but only one set of dimensions, because there is only one version of a static class. This is very important to understand.

Instead, we can make the class non-static, and then have the CalculateArea as a utility function on this class:

public class Rectangle
{
    private int width, height;

    public Rectangle(int width, int height)
    {
        this.width = width;
        this.height = height;
    }

    public void OutputArea()
    {
        Console.WriteLine("Area output: " + Rectangle.CalculateArea(this.width, this.height));
    }

    public static int CalculateArea(int width, int height)
    {
        return width * height;
    }
}

As you can see, we have made the class non-static. We have also added a constructor, which takes a width and a height and assigns it to the instance. Then we have added an OutputArea method, which uses the static method to calculate the area. This is a fine example of mixing static members with non-static members, in a non-static class.

A common usage of static classes, although frowned upon by some people, are utility/helper classes, where you collect a bunch of useful methods, which might not belong together, but don't really seem to fit elsewhere either.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!