C#多态联系之虚方法

时间:2023-03-08 15:39:15
C#多态联系之虚方法
 class Class1
{
static void Main(string[] args)
{
YuanGong yg = new YuanGong();
JingLi jl = new JingLi();
LaoBan lb = new LaoBan(); //声明一个数组的父类
YuanGong[] ayg = { yg, jl, lb };
//遍历数组
for (int i = ; i < ayg.Length; i++)
{
ayg[i].DaKa();
}
Console.ReadKey();
} }
public class YuanGong
{
//virtual虚拟,在子类中改变后,用父类实例化子类后可以调用子类方法
public virtual void DaKa()
{
Console.WriteLine("员工九点打卡");
}
}
public class JingLi:YuanGong
{
//override 接收对应 virtual
public override void DaKa()
{
Console.WriteLine("经理十点打卡");
}
}
public class LaoBan:YuanGong
{
public override void DaKa()
{
Console.WriteLine("老板不打卡");
}
}