base的只要用法有两个,首先介绍一下base调用基类的构造方法:
代码如下:
namespace _5121
{
/// <summary>
/// 学校类
/// </summary>
class School
{
public string Name;
public int Number;
//默认构造方法
public School()
{
Name = "小张";
Number = 12;
Console.WriteLine("学生:{0},学号:{1}", Name, Number);
}
}
}
namespace _5121
{
//继承School类
class Grade:School
{
public string NameOther;
public int NumberOther;
//
public Grade( ) :base()
{
NameOther = "小王";
NumberOther = 13;
Console.WriteLine("学生:{0},学号:{1}", NameOther, NumberOther);
}
}
}
namespace _5121
{
class Program
{
static void Main(string[] args)
{
//实例化
Grade grade = new Grade();
Console.Read();
}
}
}
结果如下:
2 base可以在派生类中调用基类方法:
namespace _5121
{
/// <summary>
/// 学校类
/// </summary>
class School
{
public AddSchool(int a)
{
Console.WriteLine(a * a);
}
}
}
namespace _5121
{
//继承School类
class Grade:School
{
public AddGrade(int b):base(b)
{
}
}
}
class Program
{
static void Main(string[] args)
{
//实例化
Grade grade = new Grade();
grade.AddGrade(1);
Console.Read();
}
}
输出的结果为2