有关.NET中接口继承的一个问题 [英] A question about interface inheritance in .NET

查看:126
本文介绍了有关.NET中接口继承的一个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们在接口的IQueryable< T> 例如:

public interface IQueryable<T> : IQueryable, IEnumerable;

由于IQueryable的依次实现IEnumerable

Since IQueryable in turn implements IEnumerable:

public interface IQueryable : IEnumerable;

为什么在第一次申报,IQueryable的已经明确的实现IEnumerable呢?这是从类继承不同,你不必从一个祖父母?

why in the first declaration, IQueryable has to explicitly implement IEnumerable as well? This is different from class inheritance where you do not have to explicitly inherit from a "grandparent"?

推荐答案

这是不正确的。我怀疑你对这些类右击/在Visual Studio界面,点击转到定义,这是什么的的告诉你。如果你想真正了解发生了什么,这里有一个例子:

This is not true. I suspect you are right-clicking on these classes/interfaces in Visual Studio and clicking on "Go to definition," and this is what the metadata is telling you. If you want to really understand what's happening, here's an example:

    public interface IAddTwo
    {
        int AddTwo(int x);
    }

    public interface IAddTwoOrThree : IAddTwo
    {
        int AddThree(int y);
    }

    public class AddTwoOrThree : IAddTwoOrThree
    {
        public int AddTwo(int q)
        {
            return q + 2;
        }

        public int AddThree(int z)
        {
            return z + 3;
        }
    }

需要注意的是最后一堂课只需要实现一个接口(但如果你离开了,你得到一个编译错误两种方法。

Note that the last class only has to implement one interface (but that you get a compiler error if you leave off either method.

创建如上一个类库,通过浏览到结果DLL添加引用(而不是通过向项目的引用),并查看元数据AddTwoOrThree。你会看到

Create the above as a class library, add a reference by BROWSING to the resulting DLL (not by adding a reference to the project), and view the metadata for AddTwoOrThree. You will see

AddTwoOrThree : IAddTwoOrThree, IAddTwo

中提琴!这就是为什么你看到你所看到的MSDN上的东西。他们展示了元数据,而不是他们的codeRS写了code的方式。

Viola! This is why you see what you are seeing on MSDN. They show the metadata, not the way their coders wrote the code.

这篇关于有关.NET中接口继承的一个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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