C#:抽象类需要实现接口? [英] C#: Abstract classes need to implement interfaces?

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

问题描述

我的测试code在C#中:

My test code in C#:

namespace DSnA
{
    public abstract class Test : IComparable
    {

    }
}

导致以下编译器错误:

Results in the following compiler error:

error CS0535: 'DSnA.Test' does not implement interface member
'System.IComparable.CompareTo(object)'

由于类测试是一个抽象类,为什么编译器需要它来实现接口?不应该这个要求只是强制作为具体的类?

Since the class Test is an abstract class, why does the compiler require it to implement the interface? Shouldn't this requirement only be compulsory for concrete classes?

推荐答案

在C#中,你仍然定义方法,但你没有提供一个身体,你把它标记为抽象的。像这样:

In C# you still define the methods, but you don't provide a body and you mark it as abstract. Like so:

interface IFoo
{
    void Bar();
}

abstract class Foo : IFoo
{
    public abstract void Bar();
}

这篇关于C#:抽象类需要实现接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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