关于interface

今天想在使用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继承过来的。

posted on 2004-03-10 14:58  Meyer  阅读(662)  评论(1编辑  收藏  举报

导航