using System;namespace Wrox.ProCSharp.OOProg...{ abstract class GenericCustomer ...{ private string name; public GenericCustomer(string name) //: base(name) ...{ this.name = name; } } /**//*基类的无参数构造函数不能声明为private,因为派生类的默认的构造函数会调用无参数的基类的构造函数,除非将派生类指定调用 其它构造函数*/ //这里讨论处理不同重载寒素和一个类的层次结构 class Nevermore60Customer : GenericCustomer ...{ public string re; public uint hightCostMinutesUsed; //为派生类定义自己的构造函数?为什么要定义一个构造函数呢?默认的构造函数会调用会调用无参数的GenericCustomer的构造函数,这不是我们想要的结果/ //为什么定义两个构造函数?--不是所有的GenericCustomer都有联系人 public Nevermore60Customer(string name, string re) //----------------------------- 构造函数A : base(name) ...{ this.re = re; } /**//*关键字base表示是基类的构造函数,而不是要调用类的构造函数 而this关键字调用自己类的构造函数*/ //不是所有的GenericCustomer都有联系人,没有联系人re字段设置为<none> public Nevermore60Customer(string name) //-------------------构造函数B : this(name, "<None>") ...{ } } class test ...{ public static void Main() ...{ //这样确定了所有的构造函数,执行下面的代码检查事件链是很有益的,编译器认为它需要一个字符串的参数的构造函数,所以他确认的构造函数就是B Nevermore60Customer t1 = new Nevermore60Customer("武大郎"); //在实例化ti时,就调用B函数,之后吧控制权传送给对应的Nevermore60Customer构造函数。该函数带2个参数,分别是武大郎和<none>。在这个构造函数中, //吧控制权依次给GenericCustomer构造函数,该构造函数带一个参数,即字符串武大郎,然后这个构造函数吧控制权传送给vSystem.object默认构造函数, //现在执行这些构造函数:首先执行system.object构造函数,接着执行Nevermore60Customer构造函数,初始化name字段,然后带有两个参数的Nevermore60Customer //构造函数得到控制权,吧联系人的姓名初始化为<none>,最后执行Nevermore60Customer构造函数,该构造函数带一个参数,这个构造函数什么也不做。 Console.WriteLine(t1.hightCostMinutesUsed.ToString()); Console.WriteLine(t1.re.ToString()); Console.ReadLine(); } }}