Xan*_*rUu 1 c# inheritance overriding interface partial-classes
我有以下代码:
public partial class Root : ICustomInterface
{
public virtual void Display()
{
Console.WriteLine("Root");
Console.ReadLine();
}
}
public class Child : Root
{
public override void Display()
{
Console.WriteLine("Child");
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
Root temp;
temp = new Root();
temp.Display();
}
}
Output: "Root"
Desired output: "Child"
Run Code Online (Sandbox Code Playgroud)
当我实例化一个Root对象并调用该Display()方法时,我想显示重写方法,Child这是可能的.
我需要这个,因为我必须创建一个插件,该插件是基本代码的扩展Display(),并使Root类的方法无效并仅实现插件的方法Child
当我实例化Root对象并调用Display()方法时,我希望在Child中显示重写方法,这是可能的.
您需要创建Child该类的实例.
Root temp;
temp = new Child(); //here
temp.Display();
Run Code Online (Sandbox Code Playgroud)
目前你的对象temp持有基类的引用,它对子进程没有任何了解,因此基类的输出也是如此.