为什么在C#中的基类允许实现不受它继承一个接口的合同? [英] Why is a base class in C# allowed to implement an interface contract without inheriting from it?

查看:222
本文介绍了为什么在C#中的基类允许实现不受它继承一个接口的合同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此的C#功能偶然 - 实现接口的方法不具有的基类,从它

I've stumbled upon this "feature" of C# - the base class that implements interface methods does not have to derive from it.

例如:

public interface IContract
{
    void Func();
}

// Note that Base does **not** derive from IContract
public abstract class Base
{
    public void Func()
    {
        Console.WriteLine("Base.Func");
    }
}

// Note that Derived does *not* provide implementation for IContract
public class Derived : Base, IContract
{
}

会发生什么事是,派生神奇选秀权,一个公共方法, Base.Func ,并决定将实施 IContract.Func

What happens is that Derived magically picks-up a public method, Base.Func, and decides that it will implement IContract.Func.

这是什么神奇的背后的原因

What is the reason behind this magic?

恕我直言:这个准实施功能非常-直观,使代码检查更难。 ?你觉得

IMHO: this "quasi-implementation" feature is very-unintuitive and make code-inspection much harder. What do you think?

推荐答案

原因是,您的评论简直是不正确的:

The reason is that your comment is simply incorrect:

//不会注意的的为IContract提供实施

// Note that Derived does not provide implementation for IContract

当然它。通过遵循的逻辑。

Sure it does. Follow the logic through.


  • 须提供相应的IContract的每个成员公共成员派生。

  • 基类的所有可继承成员也是派生类中的成员;继承是这样的的定义

  • 因此派生提供IContract的实现。其继承的成员是满足要求

  • 因此,没有错误的成员。

  • Derived is required to provide a public member corresponding to each member of IContract.
  • All inheritable members of a base class are also members of a derived class; that's the definition of inheritance.
  • Therefore Derived provides an implementation for IContract; its inherited member is a member that fulfills the requirement
  • Therefore, no error.

这个功能非常-直观,使代码检查更难。你觉得呢?

this feature is very-unintuitive and make code-inspection much harder. What do you think?

我觉得你不应该使用的功能,如果你不喜欢它。如果你觉得混乱,怪异的阅读,使用此功能,则鼓励你的同事谁使用这个功能来阻止这样做的代码。

I think you shouldn't use the feature if you don't like it. If you find it confusing and weird to read code that uses this feature then encourage your coworkers who use this feature to stop doing so.

这是怎么回事功能从任何其他不同功能其中从派生类中使用从基类的方法?有许多不同的方式,其中可以使用或提及从基类的方法在派生类 - 方法调用,替换,方法组转换,等等

How is this feature different from any other feature where a method from a base class is used from a derived class? There are a number of different ways in which a method from a base class may be used or mentioned in a derived class -- method calls, overrides, method group conversions, and so on.

此外,这是相对来说简单,顺利的情况下。如果你真的想抱怨C#混乱接口语义,我会花我的时间抱怨的接口重新实现语义的。那是一个真正似乎烤人的面条。我总是要看看那个东西了在规范,以确保我得到正确的语义。

Furthermore, this is relatively speaking a simple, straightforward case. If you really want to complain about confusing interface semantics in C#, I'd spend my time complaining about interface reimplementation semantics. That's the one that really seems to bake people's noodles. I always have to look that thing up in the spec to make sure I'm getting the semantics right.

这篇关于为什么在C#中的基类允许实现不受它继承一个接口的合同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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