当前位置:网站首页>C# - base 关键字用法
C# - base 关键字用法
2022-07-18 15:02:00 【wumingxiaoyao】
引言
看框架代码时,经常会看到 base
关键字,今天了解一下 C# base
关键字的用法,比较基础,通过 2 个实例有助于理解,参考官网 base。
内容提要:
- base 关键字总括
- 实例 1 访问基类方法
- 实例 2 调用基类构造函数
base 关键字总括
base
关键字用于从派生类中访问基类的成员:
- 调用基类上已被实现的方法。
- 指定创建派生类实例时应调用的基类构造函数。
仅允许用于访问基类的构造函数、实例方法或实例属性访问器。从静态方法中使用 base 关键字是错误的
。所访问的基类是类声明中指定的基类。 例如,如果指定 class ClassB : ClassA,则从 ClassB 访问 ClassA 的成员,而不考虑 ClassA 的基类。
实例 1 访问基类方法
基类 Person 和派生类 Employee 都有一个名为 Getinfo 的方法。 通过使用 base
关键字,可以从派生类中调用基类的 Getinfo 方法。
public class Person
{
protected string ssn = "444-55-6666";
protected string name = "John L. Malgraine";
public virtual void GetInfo()
{
Console.WriteLine("Name: {0}", name);
Console.WriteLine("SSN: {0}", ssn);
}
}
class Employee : Person
{
public string id = "ABC567EFG";
public override void GetInfo()
{
// Calling the base class GetInfo method:
base.GetInfo();
Console.WriteLine("Employee ID: {0}", id);
}
}
class TestClass
{
static void Main()
{
Employee E = new Employee();
E.GetInfo();
}
}
/* Output Name: John L. Malgraine SSN: 444-55-6666 Employee ID: ABC567EFG */
实例 2 调用基类构造函数
本示例显示如何指定在创建派生类实例时调用的基类构造函数。base
用于用户父类构造函数,this
用于调用自己的构造函数。有关 this
的用法,请参考 C# - this 的用法
public class BaseClass
{
int num;
public BaseClass()
{
Console.WriteLine("in BaseClass()");
}
public BaseClass(int i)
{
num = i;
Console.WriteLine("in BaseClass(int i)");
}
public int GetNum()
{
return num;
}
}
public class DerivedClass : BaseClass
{
// This constructor will call BaseClass.BaseClass()
public DerivedClass() : base()
{
}
// This constructor will call BaseClass.BaseClass(int i)
public DerivedClass(int i) : base(i)
{
}
static void Main()
{
DerivedClass md = new DerivedClass();
DerivedClass md1 = new DerivedClass(1);
}
}
/* Output: in BaseClass() in BaseClass(int i) */
边栏推荐
- Kubectl 常用命令以及简单的解释
- 【服务器数据恢复】服务器存储共享文件夹丢失数据恢复案例
- 关于面试找工作、工作中注意点的个人总结
- Anyu has not released the new platform link (continuous update)
- C# 功能收集
- Precautions for database index use and index failure [MySQL]
- 【成才之路】小杨带你刷牛牛系列之C语言编程训练~day2
- How to prepare project blueprint report materials
- 学习周刊-总第62期-一个优秀开源的的论坛程序与免费gif图片压缩工具
- 深圳“毕业”生灵活就业社保缴纳方式
猜你喜欢
什么是通用寄存器?
【AR隔空手势交互】Unity中基于Manomotion实现隔空手势交互
slurmrestd服务部署文档的撰写
Flexible employment social security payment method for Shenzhen "graduate" students
虚幻引擎UE4加载GIS数据《数字孪生&智慧城市》
硬盘格式化的数据恢复软件用哪个好?
PMP每日一练 | 考试不迷路-7.18
The new module imported in idea cannot be recognized
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(6)-Fiddler状态面板详解
Mode02 mode management module and responsibilities
随机推荐
同花顺在线开户安全吗
win11升级后开机一直在转如何解决
Which MSB is the most significant bit?
虚幻引擎UE4加载GIS数据《数字孪生&智慧城市》
How will the semiconductor market be affected by the 6.9-magnitude earthquake in Hokkaido, Japan?
seata的AT模式可以支持 kingbase 数据库吗?
PMP practice once a day | don't get lost in the exam -7.18
新增一个复合主键的步骤
QT realizes file transmission function (based on TCP) [client to server]
MediaTek 5g prototype was officially exposed and was equipped with multiple fans!
其他所得要缴个人所得税有哪九类
vs2019导出导入配置
兄弟组件通信-eventBus
redhat8 KVM部署和实现桥接网络
[proteus simulation] stc15 MCU + LCD1602 + 5x8 dot matrix character display
2022年微信小程序授权登录的最新实现方案wx.getUserProfile
堆栈的存取原则是什么?
[number theory] minimum sum of least common multiples [minimum sum LCM, uva10791]
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(6)-Fiddler状态面板详解
The Chinese company behind the 337 investigation application launched against Dajiang!