一:基类和派生类中都无定义的构造函数,构造函数初始化顺序是private string name-〉public int age,在这里由于private string name无自定义的构造函数进行初始化,故其使用默认值为Null:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.  
  6. namespace Demo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Name name = new People();  
  13.             Console.WriteLine("Name is: "+name.ToString());  
  14.             Console.ReadLine();  
  15.         }  
  16.     }  
  17.     public class Name   
  18.     {  
  19.         private string name;  
  20.  
  21.         public override string ToString()  
  22.         {  
  23.             return name;  
  24.         }  
  25.     }  
  26.  
  27.     public class People : Name   
  28.     {  
  29.         public int age;  
  30.  
  31.         public void Output()   
  32.         {  
  33.             Console.WriteLine(age);  
  34.         }  
  35.     }  
  36. }  

结果为空值:

二:基类和派生类中都有定义的无参数构造函数,构造函数初始化顺序是private string name-〉public int age,在这里由于private string name有自定义的无参数构造函数进行初始化,故其使用初始化值为“Hello”:

 

 
  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.  
  6. namespace Demo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Name name = new People();  
  13.             Console.WriteLine("Name is: "+name.ToString());  
  14.             Console.ReadLine();  
  15.         }  
  16.     }  
  17.     public abstract class Name   
  18.     {  
  19.         private string name;  
  20.  
  21.         public Name()   
  22.         {  
  23.             this.name = "Hello";  
  24.         }  
  25.  
  26.         public override string ToString()  
  27.         {  
  28.             return name;  
  29.         }  
  30.     }  
  31.  
  32.     public class People : Name   
  33.     {  
  34.         public int age;  
  35.  
  36.         public People()   
  37.         {  
  38.             this.age = 5;  
  39.         }  
  40.  
  41.         public void Output()   
  42.         {  
  43.             Console.WriteLine(age);  
  44.         }  
  45.     }  
  46. }  

结果“Hello”:

三:基类和派生类中都有定义的有参数构造函数,构造函数初始化顺序是private string name-〉public int age,在这里由于People构造函数会试图调用Name的无参数构造函数,所以这里会产生一个编译错误:'Demo.Name' does not contain a constructor that takes 0 arguments。所以这里要为People添加一个构造函数。详情,见代码:

 

 
  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.  
  6. namespace Demo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Name name = new People("Hello");  
  13.             Console.WriteLine(name.ToString());  
  14.             People people = new People("Hello");  
  15.             people.Output();  
  16.             Console.ReadLine();  
  17.         }  
  18.     }  
  19.     public abstract class Name   
  20.     {  
  21.         private string name;  
  22.  
  23.         public Name(string name)   
  24.         {  
  25.             this.name = name;  
  26.         }  
  27.  
  28.         public override string ToString()  
  29.         {  
  30.             return "Name is: "+name;  
  31.         }  
  32.     }  
  33.  
  34.     public class People : Name   
  35.     {  
  36.         public int age;  
  37.  
  38.         public People(string name)  
  39.             :base(name)  
  40.         {  
  41.             this.age=5;  
  42.         }  
  43.  
  44.         public void Output()   
  45.         {  
  46.             Console.WriteLine("Age "+age);  
  47.         }  
  48.     }  
  49. }  

结果: