How to make a subclass implement an interface?

2019-07-19 05:00发布

I have already created a class and a subclass and have a bunch of code in both. I've created an interface in the parent class and I have no idea how to make my child class implement the interface :/

The code is:

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

2条回答
贼婆χ
2楼-- · 2019-07-19 05:37

This is how your code should probably be laid out.

public interface ISmth
{
    void MyMethod();
}

class Car
{
}

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

There doesn't seem to be a reason to nest ISmth in Car, but if you do have one you certainly could do it.

查看更多
爷的心禁止访问
3楼-- · 2019-07-19 05:50

You need to declare MyCar like that:

class MyCar: Car, Car.ISmth {}
查看更多
登录 后发表回答