运行多态基础问题
using System;namespace ConApp4
{
/// <summary>
/// InheritedMethods 的摘要说明。
/// </summary>
class InheritedMethods
{
[STAThread]
static void Main(string[] args)
{
DerivedClass dc = new DerivedClass();
BaseClass bc = dc;
bc.VirtualMethod();
bc.NonVirtualMethod();
}
}
class BaseClass
{
public virtual void VirtualMethod()
{
Console.WriteLine("VirtualMethod in BaseClass");
}
public void NonVirtualMethod()
{
Console.WriteLine("NonVirtualMethod in BaseClass");
}
}
class DerivedClass : BaseClass
{
public override void VirtualMethod()
{
Console.WriteLine("VirtualMethod in DerivedClass");
}
public new void NonVirtualMethod()
{
Console.WriteLine("NonVirtualMethod in DerivedClass");
}
}
}
上面的代码运行后结果:
VirtualMethod in DerivedClass
NonVirtualMethod in BaseClass
这个是否能说明变量的 Virtual 方法由其实际类型中的方法决定
而非 Virtual 方法调用则由其声明类方法确定 ??