静态类和抽象类
http://www.cnblogs.com/tangdebing/archive/2007/05/09/739955.html
静态类和抽象类
Posted on 2007-05-09 10:07 唐德兵 阅读(89) 评论(0) 编辑 收藏 引用 网摘 静态类和类成员用于创建无需创建类的实例就能够访问的数据和函数。静态类成员可用于分离独立于任何对象标识的数据和行为:无论对象发生什么更改,这些数据和函数都不会随之变化。当类中没有依赖对象标识的数据或行为时,就可以使用静态类。

类可以声明为static的,以指示它仅包含静态成员。不能使用 new 关键字创建静态类的实例。静态类在加载包含该类的程序或命名空间时由 .NET Framework 公共语言运行库 (CLR) 自动加载。
使用静态类来包含不与特定对象关联的方法。例如,创建一组不操作实例数据并且不与代码中的特定对象关联的方法是很常见的要求。您应该使用静态类来包含那些方法。
静态类的主要功能如下:
-
它们仅包含静态成员。
-
它们不能被实例化。
-
它们是密封的。
-
它们不能包含实例构造函数(C# 编程指南)。
因此创建静态类与创建仅包含静态成员和私有构造函数的类大致一样。私有构造函数阻止类被实例化。
使用静态类的优点在于,编译器能够执行检查以确保不致偶然地添加实例成员。编译器将保证不会创建此类的实利。
静态类是密封的,因此不可被继承。静态类不能包含构造函数,但仍可声明静态构造函数以分配初始值或设置某个静态状态。有关更多信息,请参见静态构造函数(C# 编程指南)。

假设有一个类 CompanyInfo,它包含用于获取有关公司名称和地址信息的下列方法。
class CompanyInfo
{
public string GetCompanyName() { return "CompanyName"; }
public string GetCompanyAddress() { return "CompanyAddress"; }
//...
}
不需要将这些方法附加到该类的具体实例。因此,您可以将它声明为静态类,而不是创建此类的不必要实例,如下所示:
static class CompanyInfo
{
public static string GetCompanyName() { return "CompanyName"; }
public static string GetCompanyAddress() { return "CompanyAddress"; }
//...
}
使用静态类作为不与特定对象关联的方法的组织单元。此外,静态类能够使您的实现更简单、迅速,因为您不必创建对象就能调用其方法。以一种有意义的方式组织类内部的方法(例如 System 命名空间中的 Math 类的方法)是很有用的。

即使没有创建类的实例,也可以调用该类中的静态方法、字段、属性或事件。如果创建了该类的任何实例,不能使用实例来访问静态成员。只存在静态字段和事件的一个副本,静态方法和属性只能访问静态字段和静态事件。静态成员通常用于表示不会随对象状态而变化的数据或计算;例如,数学库可能包含用于计算正弦和余弦的静态方法。
在成员的返回类型之前使用 static 关键字来声明静态类成员,例如:
public class Automobile
{
public static int NumberOfWheels = 4;
public static int SizeOfGasTank
{
get
{
return 15;
}
}
public static void Drive() { }
public static event EventType RunOutOfGas;
//other non-static fields and properties...
}
静态成员在第一次被访问之前并且在任何静态构造函数(如调用的话)之前初始化。若要访问静态类成员,应使用类名而不是变量名来指定该成员的位置。例如:
Automobile.Drive();
int i = Automobile.NumberOfWheels;

下面是一个静态类的示例,它包含两个在摄氏温度和华氏温度之间执行来回转换的方法:
public static class TemperatureConverter
{
public static double CelsiusToFahrenheit(string temperatureCelsius)
{
// Convert argument to double for calculations.
double celsius = System.Double.Parse(temperatureCelsius);
// Convert Celsius to Fahrenheit.
double fahrenheit = (celsius * 9 / 5) + 32;
return fahrenheit;
}
public static double FahrenheitToCelsius(string temperatureFahrenheit)
{
// Convert argument to double for calculations.
double fahrenheit = System.Double.Parse(temperatureFahrenheit);
// Convert Fahrenheit to Celsius.
double celsius = (fahrenheit - 32) * 5 / 9;
return celsius;
}
}
class TestTemperatureConverter
{
static void Main()
{
System.Console.WriteLine("Please select the convertor direction");
System.Console.WriteLine("1. From Celsius to Fahrenheit.");
System.Console.WriteLine("2. From Fahrenheit to Celsius.");
System.Console.Write(":");
string selection = System.Console.ReadLine();
double F, C = 0;
switch (selection)
{
case "1":
System.Console.Write("Please enter the Celsius temperature: ");
F = TemperatureConverter.CelsiusToFahrenheit(System.Console.ReadLine());
System.Console.WriteLine("Temperature in Fahrenheit: {0:F2}", F);
break;
case "2":
System.Console.Write("Please enter the Fahrenheit temperature: ");
C = TemperatureConverter.FahrenheitToCelsius(System.Console.ReadLine());
System.Console.WriteLine("Temperature in Celsius: {0:F2}", C);
break;
default:
System.Console.WriteLine("Please select a convertor.");
break;
}
}
}

Please select the convertor
1. From Celsius to Fahrenheit.
2. From Fahrenheit to Celsius.
:2
Please enter the Fahrenheit temperature: 98.6
Temperature in Celsius: 37.00
附加的示例输出可能如下所示:
Please select the convertor
1. From Celsius to Fahrenheit.
2. From Fahrenheit to Celsius.
:1
Please enter the Celsius temperature: 37.00
Temperature in Fahrenheit: 98.60

参考
class(C# 参考)实例构造函数(C# 编程指南)
概念
C# 编程指南类(C# 编程指南)
抽象类
抽象类与接口紧密相关,它们不能示例化,并且常常部分实现或根本不实现。抽象类和接口之间的一个主要差别是:类可以实现无限个接口,但仅能从一个抽象(或任何其他类型)类继承。从抽象类派生的类仍可实现接口。可以在创建组件时使用抽象类,因为它们使您得以在某些方法中指定不变级功能,但直到需要该类的特定实现之后才实现其他方法。抽象类也制定版本,因为如果在派生类中需要附加功能,则可以将其添加到基类而不中断代码。
在 Visual Basic 中,抽象类用 MustInherit 关键字表示。在 C# 中,则使用 abstract 修饰符。任何所谓不变的方法都可以编码至基类中,但在 Visual Basic 中,任何要实现的方法都用 MustOverride 修饰符标记。在 C# 中,这些方法标记为 abstract。下例显示了一个抽象类:
' Visual Basic
Public MustInherit Class WashingMachine
Sub New()
' Code to instantiate the class goes here.
End sub
Public MustOverride Sub Wash
Public MustOverride Sub Rinse (loadSize as Integer)
Public MustOverride Function Spin (speed as Integer) as Long
End Class
// C#
abstract class WashingMachine
{
public WashingMachine()
{
// Code to initialize the class goes here.
}
abstract public void Wash();
abstract public void Rinse(int loadSize);
abstract public long Spin(int speed);
}
在上面的示例中,用一个已实现的方法和三个未实现的方法声明抽象类。从该类继承的类必须实现 Wash、Rinse 和 Spin 方法。下例显示了该类实现的可能形式:
' Visual Basic
Public Class MyWashingMachine
Inherits WashingMachine
Public Overrides Sub Wash()
' Wash code goes here
End Sub
Public Overrides Sub Rinse (loadSize as Integer)
' Rinse code goes here
End Sub
Public Overrides Function Spin (speed as Integer) as Long
' Spin code goes here
End Sub
End Class
// C#
class MyWashingMachine : WashingMachine
{
public MyWashingMachine()
{
// Initialization code goes here.
}
override public void Wash()
{
// Wash code goes here.
}
override public void Rinse(int loadSize)
{
// Rinse code goes here.
}
override public long Spin(int speed)
{
// Spin code goes here.
}
}
在实现抽象类时,必须实现该类中的每一个抽象 (MustOverride) 方法,而每个已实现的方法必须和抽象类中指定的方法一样,接收相同数目和类型的参数,具有同样的返回值。