如何让子类实现接口?
How to make a subclass implement an interface?
我创建了一个 class 和一个子 class。我在parentclass也有一个接口。如何让 child class 实现接口?
密码是:
class Car
{
public interface ISmth
{
void MyMethod();
}
}
class MyCar : Car
{
//implement the interface
}
您需要这样声明 MyCar:
class MyCar: Car, Car.ISmth {}
这就是您的代码的布局方式。
public interface ISmth
{
void MyMethod();
}
class Car
{
}
class MyCar : Car, ISmth
{
public void MyMethod()
{
}
}
似乎没有理由将 ISmth
嵌套在 Car
中,但如果你有一个,你当然可以做到。
我创建了一个 class 和一个子 class。我在parentclass也有一个接口。如何让 child class 实现接口?
密码是:
class Car
{
public interface ISmth
{
void MyMethod();
}
}
class MyCar : Car
{
//implement the interface
}
您需要这样声明 MyCar:
class MyCar: Car, Car.ISmth {}
这就是您的代码的布局方式。
public interface ISmth
{
void MyMethod();
}
class Car
{
}
class MyCar : Car, ISmth
{
public void MyMethod()
{
}
}
似乎没有理由将 ISmth
嵌套在 Car
中,但如果你有一个,你当然可以做到。