如何使子类实现接口? [英] How to make a subclass implement an interface?

查看:209
本文介绍了如何使子类实现接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类和一个子类。我在父类中也有一个接口。如何使子类实现该接口?

I have created a class and a subclass. I also have an interface in the parent class. How can I make the child class implement the interface?

代码为:

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


推荐答案

这应该是您代码的布局方式。

This is how your code should probably be laid out.

public interface ISmth
{
    void MyMethod();
}

class Car
{
}

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

没有似乎是将 ISmth 嵌套在 Car 车中的原因,但是如果您有,肯定可以做到。

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

这篇关于如何使子类实现接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆