我有以下代码:强制重写方法基类
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"
当我实例化一个Root
对象并调用Display()
方法我想在Child
显示重写的方法这是可能的。
我需要这个,因为我必须创建一个插件,是一个扩展,基本代码和空洞的Root
类的Display()
方法,当我实例化一个根对象和呼叫仅实现插件的方法Child
我不认为你完全理解继承。如果您希望Display()方法输出“Child”,您需要创建一个Child实例。 – 2013-04-04 09:11:03
对于您编辑的部分问题,您应该看到:http://stackoverflow.com/questions/2779146/c-is-there-它是继承的方法 - 类 - 去除 - 方法,也是这样一个:http:// stackoverflow。com/questions/1125746/how-to-hide-remove-a-base-classs-methods-in-c – Habib 2013-04-04 09:25:54