C#学习笔记整理_深入剖析构造函数、析构函数

2019-12-30 14:02:57王振洲


class Car

{

  -Car() 

  {

    // … …

  }

}

该析构函数隐式地对继承链中的所有实例递归地调用调用 Finalize()方法


public class Bus
 {
   protected static readonly DateTime globalStartTime;
 
   protected int RouteNumber { get; set; }
 
   static Bus() //静态构造函数
   {
 globalStartTime = DateTime.Now;
 Console.WriteLine("Static ctor sets global start time to {0}", globalStartTime.ToLongTimeString());
   }
 
   public Bus(int routeNum)
   {
 RouteNumber = routeNum;
 Console.WriteLine("{0} is created.", RouteNumber);
   }
 
   public void Drive()
   {
 TimeSpan elapsedTime = DateTime.Now - globalStartTime;
 
 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()
   {
 
 Bus bus = new Bus(71);
 bus.Drive();
 System.Threading.Thread.Sleep(25);
 
 Bus bus2 = new Bus(72);
 bus2.Drive();
 
 System.Console.WriteLine("Press any key to exit.");
 System.Console.ReadKey();
   }
 }
 /* Output:
   Static ctor sets global start time to 10:04:08 AM
   71 is created.
   71 is starting its route 21.00 minutes after global start time 10:04 AM.
   72 is created.
   72 is starting its route 46.00 minutes after global start time 10:04 AM.   
*/

以上就是小编为大家带来的C#学习笔记整理_深入剖析构造函数、析构函数的全部内容了,希望对大家有所帮助,多多支持ASPKU~


注:相关教程知识阅读请移步到c#教程频道。