子类中调用父类方法

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace abstract_class
 7 {
 8     abstract class my_abstract_class
 9     {
10         public abstract void abstract_method();
11     }
12     class my_class : my_abstract_class
13     {
14         public override void abstract_method()
15         {
16             Console.WriteLine("this is a method of my class");
17         }
18     }
19     class my_sub_class : my_class
20     {
21         //public override void abstract_method()
22         //{
23         //    base.abstract_method();
24         //    Console.WriteLine("this is a method of my sub class");
25         //}
26         public void general()
27         {
28             abstract_method();
29         }
30     }
31 }
  • 在子类中调用父类的方法,如果该方法被子类重载,则会调用子类方法。
  • 如果已经重载则需要base关键字来标定调用的是哪个方法。
  • 如果未被重载,则会调用父类方法,不需要base关键字。
  • 调用父类方法不需要base关键字,不会报错,但base关键字可以明确的表示调用的是父类方法。
原文地址:https://www.cnblogs.com/henyihanwobushi/p/2590470.html