C#基础之 派生类

1: 当创建派生类的实例时,会自动调用基类的默认构造函数

 1 namespace parentTest
 2 {
 3     public class Reader
 4     {
 5         public Reader()
 6         {
 7             Console.WriteLine("基类的构造函数被调用");
 8         }
 9     
10     }
11     public class Student:Reader
12     {
13         public Student()
14         {
15             Console.WriteLine("派生类的构造函数被调用");
16         }
17     }
18 
19     class Program
20     {
21            static void Main(string[] args)
22         {
23             Student stu = new Student();  //结果将先 调用 基类的构造函数,在调用 派生类的构造函数
24         }
25     }
26 }

结果:

C#基础之 派生类

2:

1       public Student()   //这种方法属于基类默认构造函数的隐式调用
2         {
3             //方法体
4         }
5         public Student()   //这种方法属于基类默认构造函数的  显示调用
6             : base()
7         {
8             //方法体
9         }

3: 显示调用  基类的其他构造函数

 1 namespace parentTest
 2 {
 3     public class Reader
 4     {
 5         public string ReaderID { get; set; }
 6         public string ReaderName { get; set; }
 7 
 8         public Reader(string readerid, string readername)
 9         {
10             this.ReaderID = readerid;
11             this.ReaderName = readername;
12             Console.WriteLine("基类的带参构造函数被调用");
13         }   
14     }
15 
16     public class Student:Reader
17     {
18         public string ClassRoom{get;set;}
19 
20         public Student(string readerid, string readername, string classroom)
21             : base(readerid, readername)  //注意此处
22             
23         {
24             this.ClassRoom = classroom;
25             Console.WriteLine("派生类的带参数构造函数被调用了");
26             Display();
27         }
28         public void Display()
29         {
30             Console.WriteLine("读者证号:" + ReaderID);  //其中 ReaderID 和 ReaderName 的值由  base(readerid, readername)  中继承
31             Console.WriteLine("读者姓名:" + ReaderName);
32             Console.WriteLine("读者班级:" + ClassRoom);
33         }
34     }
35 
36     class Program
37     {
38            static void Main(string[] args)
39         {
40             Student stu = new Student("s123", "tang", "a1"); //结果将先 调用 基类的构造函数,在调用 派生类的构造函数
41    
42         }
43     }
44 }

结果:

C#基础之 派生类

4:隐藏基类的成员

 1 namespace parentTest
 2 {
 3     public class BaseClass
 4     {
 5         public string name = "Tang";
 6         public void Method()
 7         {
 8             Console.WriteLine("调用的是基类的方法");
 9         }
10     }
11 
12     public class DerivedClass : BaseClass
13     {
14         //若想在派生类中 重新命名 name 的值,使用下面方式  
15         public new string name = "newTang";  //关键字 new
16 
17         public new void Method()  //此处隐藏了基类的 Method() 方法
18         {
19             Console.WriteLine("调用的是派生类的方法");
20         }
21 
22     }
23 
24     class Program
25     {
26         static void Main(string[] args)
27         {
28             //调用基类的方法
29             BaseClass bc = new BaseClass();
30             Console.WriteLine(bc.name);
31             bc.Method();
32 
33             //调用派生类的方法
34             DerivedClass dc = new DerivedClass();
35             Console.WriteLine(dc.name);
36             dc.Method();
37         }
38     }
39 
40 }

结果:

C#基础之 派生类

5: 重写基类的方法

将基类中的某个方法声明为 virtual,则该方法称为虚方法,基类中的虚方法能在派生类中被重写(使用override)

 1 namespace parentTest
 2 {
 3     public class BaseClass
 4     {
 5         public virtual void Method()  //注意 因为要在派生类中重写,所以修饰符不能使用 private
 6         {
 7             Console.WriteLine("调用的是基类的方法");
 8         }
 9     }
10 
11     public class DerivedClass : BaseClass
12     {
13         //在派生类中重写基类的方法
14         public override void Method()
15         {
16             Console.WriteLine("调用派生类方法");
17         }
18     }
19 
20     class Program
21     {
22         static void Main(string[] args)
23         {
24             DerivedClass dc = new DerivedClass();
25             dc.Method();
26         }
27     }
28 }

结果:

C#基础之 派生类