如何使一个子类实现一个接口?
问题描述:
我已经创建了一个类和一个子类,并且都有一堆代码。我创建了一个父类的接口,我不知道如何让我的子类实现的接口:/如何使一个子类实现一个接口?
的代码是:
class Car {
public interface ISmth
{
void MyMethod();
}
}
class MyCar : Car {
//implement the interface
}
答
这就是你的代码应该如何布局。
public interface ISmth
{
void MyMethod();
}
class Car
{
}
class MyCar : Car, ISmth
{
public void MyMethod()
{
}
}
似乎有不是一个理由窝ISmth
在Car
,但如果你有一个你肯定可以做到这一点。
答
需要声明MyCar这样的:
class MyCar: Car, Car.ISmth {}