如果你有课:
public class Car {
public static readonly int Wheels = 4;
public static int Count {get;set;}
public string Make {get;set;}
public string Model {get;set;}
public int Year {get;set;}
public Car() { Car.Count = Car.Count + 1; }
public string SoundOff(){
return String.Format("I am only 1 out of {0} car{1}",
Car.Count, (Car.Count > 0 ? "s" : String.Empty));
}
}
然后,每次创建汽车时,计数都会增加一。这是因为 Count 属性属于 Car 类,而不是您创建的任何对象。
这也很有用,因为每辆车都可以知道Car.Count。所以,如果你创建了:
Car sportster = new Car {
Make="Porsche", Model="Boxster", Year=2010 };
sportster.SoundOff(); // I am only 1 out of 1 car
您可以进行其他处理,所有对象都会知道 Count:
Car hybrid = new Car { Make="Toyota", Model="Prius", Year=2010 };
hybrid.SoundOff(); // I am only 1 out of 2 cars
sportster.SoundOff(); // I am only 1 out of 2 cars
所以,换句话说,当你想要某样东西时,你应该使用静态:
- 可在类级别访问,以便所有对象都知道它(
Car.Count 而不是 hybrid.Count)
- 代表类而不是对象(
Car.Wheels 不会改变)
还有其他使用静态的原因,例如实用程序类、扩展方法等。但这应该可以回答您关于 MSDN 措辞的问题。