C#基础语法:Base关键字学习笔记

所属分类: 软件编程 / C#教程 阅读数: 135
收藏 0 赞 0 分享

它与this关键字一样,都是作为类的实例(因此不能调用基类的静态成员和抽象成员)简写或者替代而存在的,只不过this关键字用于替代本类的实例,base关键字用于替代基类的实例,用法很简单,其访问基类的形式如下: 

base.【标识符】

base[【表达式列表】]      这个类型的一看便可以大概猜测多用于基类实例的索引器操作,在我下面演示的代码中你会看到它的用法。

对于  base.【标识符】的访问形式再次说明一下:

对于非虚方法,这种访问仅仅是对基类实例成员的直接访问,完全等价于((base)this).【标识符】。

对于虚方法,对于这种访子类重写该虚方法运用这种访问形式也是(禁用了虚方法调用的机制)对基类实例成员的直接访问,将其看做非虚方法处理,此时则不等价于((base)this).【标识符】,因为这种格式完全遵守虚方法调用的机制,其声明试时为积累类型,运行时为子类类型,所以执行的还是子类的重写方法。于未重写的虚方法等同于简单的非虚方法处理。

测试代码如下:

using System;

namespace BaseTest
{
  class father
  {
    string str1 = "this field[1] of baseclass", str2 = "this field[2] of baseclass";
    public void F1() //Non-virtual method
    {
      Console.WriteLine(" F1 of the baseclass");
    }
    public virtual void F2()//virtual method
    {
      Console.WriteLine(" F2 of the baseclass");
    }
    public virtual void F3()
    {
      Console.WriteLine(" F3 of the baseclass that is not overrided "); 
    }
    public string this[int index]
    {
      set
      {
        if (index==1 )
        {
          str1 = value;
        }
        else
        {
          str2 = value;
        }
      }
      get
      {
        if (index ==1)
        {
          return str1;
        }
        else
        {
          return str2;
        }
      }
    }
  }
  class Child:father
  {
    public void G()
    {
      Console.WriteLine("======Non-virtual methods Test =========");
      base.F1();
      ((father)this).F1();
      Console.WriteLine("======virtual methods Test=========");
      base.F2();
      ((father)this).F2();
      base.F3();
      ((father)this).F3();
      Console.WriteLine("=====Test the type that the tbase [[expression]] ==========");
      Console.WriteLine(base[1]);
      base[1] = "override the default ";
      Console.WriteLine(base[1]);
      Console.WriteLine("================Test Over=====================");
    }
    public override void F2()
    {
      Console.WriteLine(" F2 of the subclass ");
    }
    
    static void Main(string[] args)
    {
      Child child=new Child();
      child.G();
      Console.ReadKey();
    }
  }
}


base用于构造函数声明,用法和this用于构造函数声明完全一致,但base是对基类构造函数形参的匹配。

 

using System;

namespace BaseCoTest
{
  class Base
  {
    public Base(int a, string str)
    {
      Console.WriteLine("Base. Base(int a,string str)");
    }
    public Base(int a)
    {
      Console.WriteLine("Base. Base(int a)");
    }
    public Base()
    {
    }
  }
  class Sub : Base
  {
    public Sub()
    {
    }
    public Sub(int a)
      : base(1, "123")
    {
      Console.WriteLine("Sub .Sub(int a)");
    }
    class Test
    {
      public static void Main()
      {
        Sub sub = new Sub(1);
        Console.ReadKey();
      }
    }
  }
}

更多精彩内容其他人还在看

c#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多