C# 关键字:base

1、显示调用父类的构造函数

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Teacher t = new Teacher();
 6 
 7             Console.ReadKey();
 8         }
 9     }
10 
11     class Person
12     {
13         public Person()
14         {
15             Console.WriteLine("我是人类");
16         }
17     }
18 
19     class Teacher : Person
20     {
21         public Teacher() : base()
22         {
23             Console.WriteLine("我是老师");
24         }
25     }

运行结果:

C# 关键字:base

2、调用父类的成员

 1      class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Teacher t = new Teacher();
 6             t.SayHello();
 7 
 8             Console.ReadKey();
 9         }
10     }
11 
12     class Person
13     {
14         public Person()
15         {
16             Console.WriteLine("我是人类");
17         }
18 
19         public virtual void SayHello()
20         {
21             Console.WriteLine("Hello");
22         }
23     }
24 
25     class Teacher : Person
26     {
27         public Teacher() : base()
28         {
29             Console.WriteLine("我是老师");
30         }
31 
32         public override void SayHello()
33         {
34             base.SayHello();
35             Console.WriteLine("World");
36         }
37     }

运行结果:

C# 关键字:base

 

 

 

C# 关键字:base

上一篇:MS-API。AJAS


下一篇:C# 关键字:new