在 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.
}
}
通過這種方式,您可以實現類似于虛擬類功能的效果。