在C#中base关键字的概念非常简单。
当我们要从派生类中访问基类的成员和功能时,使用关键字base。
在以下示例中,基类,所有者和派生类,员工,具有命名信息的方法。
通过使用基本关键字,可以从派生类中调用基类上的Info方法。
C#中base关键字示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace this_example
{
public class Owner
{
protected string pinname = "78";
protected string name = "rahul";
public virtual void Info()
{
Console.WriteLine("Name: {0}", name);
Console.WriteLine("pinname: {0}", pinname);
}
}
class Employee : Owner
{
public string id = "ABC567";
public override void Info()
{
//Calling the base class GetInfo method:
base.Info();
Console.WriteLine("Employee ID: {0}", id);
}
}
class TestClass
{
public static void Main()
{
Employee E = new Employee();
E.Info();
Console.ReadLine();
}
}
}
日期:2020-04-11 22:50:14 来源:oir作者:oir
