20.C#编程指南-构造函数
2012-05-19 23:20
225 查看
使用构造函数
任何时候,只要创建类或结构,就会调用它的构造函数。如果您没有为对象提供构造函数,则默认情况下C#将创建一个构造函数,该构造函数实例化对象,并将成员变量设置为默认值表中列出的默认值。静态类和结构也可以有构造函数。
如果您为对象提供了构造函数,C#将不会创建默认构造函数(不带参数的构造函数)。
类和 structs 都可以定义具有参数的构造函数。 带参数的构造函数必须通过 new 语句或 base 语句来调用。 类和 structs 还可以定义多个构造函数,并且二者均不需要定义默认构造函数。 例如:
View Code
任何时候,只要创建类或结构,就会调用它的构造函数。如果您没有为对象提供构造函数,则默认情况下C#将创建一个构造函数,该构造函数实例化对象,并将成员变量设置为默认值表中列出的默认值。静态类和结构也可以有构造函数。
如果您为对象提供了构造函数,C#将不会创建默认构造函数(不带参数的构造函数)。
类和 structs 都可以定义具有参数的构造函数。 带参数的构造函数必须通过 new 语句或 base 语句来调用。 类和 structs 还可以定义多个构造函数,并且二者均不需要定义默认构造函数。 例如:
View Code
public class Bus { // Static variable used by all Bus instances. // Represents the time the first bus of the day starts its route. protected static readonly DateTime globalStartTime; // Property for the number of each bus. protected int RouteNumber { get; set; } // Static constructor to initialize the static variable. // It is invoked before the first instance constructor is run. static Bus() { globalStartTime = DateTime.Now; // The following statement produces the first line of output, // and the line occurs only once. Console.WriteLine("Static constructor sets global start time to {0}", globalStartTime.ToLongTimeString()); } // Instance constructor. public Bus(int routeNum) { RouteNumber = routeNum; Console.WriteLine("Bus #{0} is created.", RouteNumber); } // Instance method. public void Drive() { TimeSpan elapsedTime = DateTime.Now - globalStartTime; // For demonstration purposes we treat milliseconds as minutes to simulate // actual bus times. Do not do this in your actual bus schedule program! Console.WriteLine("{0} is starting its route {1:N2} minutes after global start time {2}.", this.RouteNumber, elapsedTime.TotalMilliseconds, globalStartTime.ToShortTimeString()); } } class TestBus { static void Main() { // The creation of this instance activates the static constructor. Bus bus1 = new Bus(71); // Create a second bus. Bus bus2 = new Bus(72); // Send bus1 on its way. bus1.Drive(); // Wait for bus2 to warm up. System.Threading.Thread.Sleep(25); // Send bus2 on its way. bus2.Drive(); // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } } /* Sample output: Static constructor sets global start time to 3:57:08 PM. Bus #71 is created. Bus #72 is created. 71 is starting its route 6.00 minutes after global start time 3:57 PM. 72 is starting its route 31.00 minutes after global start time 3:57 PM. */
相关文章推荐
- 高质量C++/C编程指南 -- 第9章 类的构造函数、析构函数与赋值函数
- 静态构造函数(C# 编程指南)
- 私有构造函数(C# 编程指南)
- 使用构造函数(C# 编程指南
- 高质量C++/C编程指南 -- 第9章 类的构造函数、析构函数与赋值函数
- C# 编程指南静态构造函数(C# 编程指南)
- 静态构造函数(C# 编程指南)
- 高质量C++/C编程指南 -- 第9章 类的构造函数、析构函数与赋值函数
- 高质量C/C++编程指南 阅读笔记(四)类的构造函数、析构函数与赋值函数
- 高质量C++/C编程指南 - 第9章 类的构造函数、析构函数与赋值函数
- 高质量C++/C编程指南 -- 第9章 类的构造函数、析构函数与赋值
- 静态构造函数(C# 编程指南)
- Lambda 表达式(C# 编程指南)
- 高级bash编程指南(九)
- iOS多线程编程指南
- 《Spark 官方文档》Spark SQL, DataFrames 以及 Datasets 编程指南
- Posix线程编程指南(2)--线程私有数据
- POSIX操作系统的串口编程指南(1)
- hive编程指南--employees表数据定义
- Quartz 2D编程指南1-------Quartz 2D概述