How to make a subclass implement an interface?

孤街醉人 提交于 2020-01-04 06:31:37

问题


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 
                  }

回答1:


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.




回答2:


You need to declare MyCar like that:

class MyCar: Car, Car.ISmth {}


来源:https://stackoverflow.com/questions/35663209/how-to-make-a-subclass-implement-an-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!