码迷,mamicode.com
首页 > Windows程序 > 详细

C#中base 关键字的作用

时间:2014-11-01 16:17:24      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   使用   sp   

引用:http://msdn.microsoft.com/en-us/library/hfw7t1ce.aspx

base

base 关键字用于从派生类中访问基类的成员:

  • 调用基类上已被其他方法重写的方法。

  • 指定创建派生类实例时应调用的基类构造函数。

基类访问只能在构造函数、实例方法或实例属性访问器中进行。

从静态方法中使用 base 关键字是错误的。

 

一.在本例中,基类 Person 和派生类 Employee 都有一个名为 Getinfo 的方法。通过使用 base 关键字,可以从派生类中调用基类的 Getinfo 方法。

 1 using System;
 2 using System.Text;
 3 
 4 namespace ConsoleApplication1
 5 {
 6     class Program
 7     {
 8         public class Person
 9         {
10             protected string ssn = "444-55-6666";
11             protected string name = "John L. Malgraine";
12 
13             public virtual void GetInfo()
14             {
15                 Console.WriteLine("Name: {0}", name);
16                 Console.WriteLine("SSN: {0}", ssn);
17             }
18         }
19         class Employee : Person
20         {
21             public string id = "ABC567EFG";
22             public override void GetInfo()
23             {
24                 // Calling the base class GetInfo method:
25                 base.GetInfo();
26                 Console.WriteLine("Employee ID: {0}", id);
27             }
28         }
29 
30         static void Main(string[] args)
31         {
32             Employee E = new Employee();
33             E.GetInfo();
34             Console.ReadKey();
35         }
36     }
37 }

运行结果

bubuko.com,布布扣

说明base作用起到了从派生类中调用基类的方法。注意:在重写基类后调用基类的方法是重写后的方法了

 

二.本示例显示如何指定在创建派生类实例时调用的基类构造函数。

 1 using System;
 2 using System.Text;
 3 
 4 namespace ConsoleApplication1
 5 {
 6     class Program
 7     {
 8         public class BaseClass
 9         {
10             int num;
11 
12             public BaseClass()
13             {
14                 Console.WriteLine("in BaseClass()");
15             }
16 
17             public BaseClass(int i)
18             {
19                 num = i;
20                 Console.WriteLine("in BaseClass(int i)");
21             }
22 
23             public int GetNum()
24             {
25                 return num;
26             }
27         }
28 
29         public class DerivedClass : BaseClass
30         {
31             // This constructor will call BaseClass.BaseClass()
32             public DerivedClass()
33                 : base()
34             {
35             }
36 
37             // This constructor will call BaseClass.BaseClass(int i)
38             public DerivedClass(int i)
39                 : base(i)
40             {
41             }
42         }
43 
44         static void Main(string[] args)
45         {
46             DerivedClass md = new DerivedClass();
47             DerivedClass md1 = new DerivedClass(1);
48             Console.ReadKey();
49         }
50     }
51 }

bubuko.com,布布扣

 

C#中base 关键字的作用

标签:style   blog   http   io   color   os   ar   使用   sp   

原文地址:http://www.cnblogs.com/abc1069/p/4067215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!