base关键字的说明(学习使用)

        从本例中大家可以看出继承和重载的使用,各人感觉简明易懂icon17.gif。在第二个例子中大家可以看出如何指定在创建派生类实例时调用的基类构造函数。

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

  • 调用基类上已被其他方法重写的方法。
  • 指定创建派生类实例时应调用的基类构造函数。

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

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

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

 1 None.gif //  keywords_base.cs
 2 None.gif //  Accessing base class members
 3 None.gif using  System;
 4 None.gif    public   class  Person 
 5 ExpandedBlockStart.gifContractedBlock.gif    dot.gif {
 6InBlock.gif      protected string ssn = "444-55-6666";
 7InBlock.gif      protected string name = "John L. Malgraine";
 8InBlock.gif
 9InBlock.gif      public virtual void GetInfo() 
10ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
11InBlock.gif         Console.WriteLine("Name: {0}", name);
12InBlock.gif         Console.WriteLine("SSN: {0}", ssn);
13ExpandedSubBlockEnd.gif      }

14ExpandedBlockEnd.gif   }

15 None.gif    class  Employee: Person 
16 ExpandedBlockStart.gifContractedBlock.gif    dot.gif {
17InBlock.gif      public string id = "ABC567EFG";
18InBlock.gif
19InBlock.gif      public override void GetInfo() 
20ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
21InBlock.gif         // Calling the base class GetInfo method:
22InBlock.gif         base.GetInfo();
23InBlock.gif         Console.WriteLine("Employee ID: {0}", id);
24ExpandedSubBlockEnd.gif      }

25ExpandedBlockEnd.gif   }

26 None.gif
27 ExpandedBlockStart.gifContractedBlock.gif class  TestClass  dot.gif {
28InBlock.gif   public static void Main() 
29ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
30InBlock.gif      Employee E = new Employee();
31InBlock.gif      E.GetInfo();
32ExpandedSubBlockEnd.gif   }

33ExpandedBlockEnd.gif}

输出

Name: John L. Malgraine
SSN: 444-55-6666
Employee ID: ABC567EFG












示例

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

 1 None.gif //  keywords_base2.cs
 2 None.gif using  System;
 3 None.gif public   class  MyBase
 4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 5InBlock.gif   int num;
 6InBlock.gif
 7InBlock.gif   public MyBase() 
 8ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
 9InBlock.gif      Console.WriteLine("in MyBase()");
10ExpandedSubBlockEnd.gif   }

11InBlock.gif
12InBlock.gif   public MyBase(int i )
13ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
14InBlock.gif      num = i;
15InBlock.gif      Console.WriteLine("in MyBase(int i)");
16ExpandedSubBlockEnd.gif   }

17InBlock.gif
18InBlock.gif   public int GetNum()
19ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
20InBlock.gif      return num;
21ExpandedSubBlockEnd.gif   }

22ExpandedBlockEnd.gif}

23 None.gif
24 None.gif public   class  MyDerived: MyBase
25 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
26InBlock.gif   // This constructor will call MyBase.MyBase()
27InBlock.gif   public MyDerived() : base()
28ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
29ExpandedSubBlockEnd.gif   }

30InBlock.gif
31InBlock.gif    // This constructor will call MyBase.MyBase(int i)
32InBlock.gif   public MyDerived(int i) : base(i)
33ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
34ExpandedSubBlockEnd.gif   }

35InBlock.gif
36InBlock.gif   public static void Main() 
37ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
38InBlock.gif      MyDerived md = new MyDerived();
39InBlock.gif      MyDerived md1 = new MyDerived(1);
40ExpandedSubBlockEnd.gif   }

41ExpandedBlockEnd.gif}


输出

in MyBase()
in MyBase(int i)








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值