110,538
社区成员
发帖
与我相关
我的任务
分享
class Parent
{
public void Call()
{
Console.WriteLine("Call of Parent");
}
}
class Child:Parent
{
public void Call()
{
Console.WriteLine("Call of Child");
}
}
class test
{
public static void Main()
{
Parent child = new Child();
a.Call();
Console.ReadKey();
}
}
输出Call of Parent,这里child中的Call方法会有一个警告,要加new,加上之后输出结果一样
class Parent
{
public virtual void Call()
{
Console.WriteLine("Call of Parent");
}
}
class Child:Parent
{
public void Call()
{
Console.WriteLine("Call of Child");
}
}
class test
{
public static void Main()
{
Parent child = new Child();
child.Call();
Console.ReadKey();
}
}
虽然Parent中的Call方法改成了虚方法,但是输出结果
仍然是Call of Parent,child中的Call方法还是会有一个警告,要加new,加上之后输出结果一样
class Parent
{
public virtual void Call()
{
Console.WriteLine("Call of Parent");
}
}
class Child:Parent
{
public override void Call()
{
Console.WriteLine("Call of Child");
}
}
class test
{
public static void Main()
{
Parent child = new Child();
child.Call();
Console.ReadKey();
}
}
父类方法中Virtual,子类方法中Override
输出Call of Child