今天想在使用interface的时候,想继承实现了接口的类。
Example:
interface IFoo
{
  void Show();
}

class Base : IFoo
{
  public void Show()
  {
    Console.WriteLine("Base Show");
  }
}

class Derived : Base
{
  public override void Show()
  {
    Console.WriteLine("Derived Show");
  }
}

error CS0506: “Derived.Show()” : 无法重写继承成员“Base.Show()”,
因为它未标记为 virtual、abstract 或 override

想起以前看 Don Box的<Essential .net>时,说到C#中,实现接口的方法默认
是sealed的,记得他提到可以显示的说明不要sealed,我试了一下将Base写成

class Base : IFoo
{
  public virtual void Show()
  {
    Console.WriteLine("Base Show");
  }
}

编译、运行,正常。
C#中的,接口和虚类(abstract class)有很大的不同,从根本上说,定义的
interface就不是不是从object继承过来的。

相关文章:

  • 2022-12-23
  • 2021-07-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-15
猜你喜欢
  • 2021-12-13
  • 2022-12-23
  • 2021-10-30
  • 2022-12-23
  • 2022-12-23
  • 2021-05-30
  • 2021-08-15
相关资源
相似解决方案