如何使子类实现接口?

时间:2016-02-26 22:52:36

标签: c# class interface subclass

我已经创建了一个类和一个子类,并且两者都有一堆代码。我在父类中创建了一个接口,我不知道如何让我的子类实现接口:/

代码是:

class Car { 
              public interface ISmth
              {
                  void MyMethod();
              }
          }
class MyCar : Car { 
                      //implement the interface 
                  }

2 个答案:

答案 0 :(得分:2)

您需要像这样声明MyCar:

class MyCar: Car, Car.ISmth {}

答案 1 :(得分:2)

这就是你的代码应该如何布局。

public interface ISmth
{
    void MyMethod();
}

class Car
{
}

class MyCar : Car, ISmth
{
    public void MyMethod()
    {
    }
}

似乎没有理由在ISmth中嵌套Car,但如果你确实有{{1}},你肯定可以这样做。