在 C# 中,您不能直接创建虚拟类
public class MyBaseClass
{
public void BaseMethod()
{
Console.WriteLine("This is a method in the base class.");
}
}
public class MyDerivedClass : MyBaseClass
{
public override void BaseMethod()
{
Console.WriteLine("This is a virtual method in the derived class.");
}
}
class Program
{
static void Main(string[] args)
{
MyDerivedClass derivedObject = new MyDerivedClass();
derivedObject.BaseMethod(); // 输出:This is a virtual method in the derived class.
}
}
通过这种方式,您可以实现类似于虚拟类功能的效果。