难道一个类继承类继承类/接口另一个类继承? [英] Does a class that inherits another class inherit that classes inherited classes/interfaces?

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

问题描述

我有两个类:

public class Question : IDisposable, IEquatable<Question>
{
}

public class SessionQuestion : Question, IDisposable, IEquatable<SessionQuestion>
{
}



由于问题继承的IDisposable IEquatable ,确实 SessionQuestion ?隐式继承这些接口以及

As Question inherits IDisposable and IEquatable, does SessionQuestion implicitly inherit those interfaces as well?

推荐答案

答案是是 - 接口继承。但是,是否可以忽略掉接口定义从一个类依赖于类是否想拥有某个接口成员的显式接口实现

The answer is "Yes" -- interfaces are inherited. However, whether you can omit including interface definitions from a class depends on whether that class wants to have certain interface members as explicit interface implementations.

让我们这个非常简单(傻)例如:

Let's take this very simple (and silly) example:

public interface IMyInterface
{
    void foo();
}

public class A : IMyInterface
{
    public void foo()
    {
        Console.Out.WriteLine("A.foo() executed");
    }
}

public class B : A, IMyInterface
{
    void IMyInterface.foo()
    {
        Console.Out.WriteLine("B.foo() executed");
    }
}



由于B都想拥有的的美孚( )的方法作为显式接口实现,在 IMyInterface的的接口被指定为它的类定义的一部分 - 即便A类已实现此接口。从基类继承的接口声明是不是在这种情况下,就足够了。

Since B wants to have the foo() method as an explicit interface implementation, the IMyInterface interface has to be specified as part of its class definition -- even if class A already implements this interface. Inheriting an interface declaration from a base class is not sufficient in this case.

如果B类不会有显式接口实现,那么B类并不需要指定接口(在这个小例子的 IMyInterface的的)一次。

If class B would not have explicit interface implementations, then class B does not need to specify the interface (IMyInterface in this little example) again.



继承从基类的接口,并有一些显式接口实施该接口的成员可能会导致影响其充其量是令人惊讶的,在最坏的情况可能会导致断/ bug的软件。


Inheriting an interface from a base class and having some explicit interface implementation for members of that interface can lead to effects which can be surprising at best, and at worst can lead to broken/buggy software.

如果您执行以下代码序列

If you execute the following code sequence:

A obj = new A();
obj.foo();
IMyInterface x = obj;
x.foo();



输出会

the output will be

A.foo()执行

A.foo()

A.foo() executed
A.foo() executed



然而,让我们现在使用b类的对象,但是,否则保持代码完全相同的:


However, let's now use an object of type B but otherwise keep the code exactly the same:

B obj = new B();
obj.foo();
IMyInterface x = obj;
x.foo();



输出将有所不同:

the output will be somewhat different:

A.foo()执行

B.foo()执行

A.foo() executed
B.foo() executed

为什么会这样?请记住,B类继承的实施的富()的从类A.方法。因此,调用 obj.foo()仍将执行继承的富()的方法。

Why is that so? Remember that class B inherits the implementation of the foo() method from class A. Thus, calling obj.foo() will still execute the inherited foo() method.

现在,为什么那么 x.foo()不调用的富()的由A类提供,以及实施?或者说,为什么是 obj.foo()不是调用的富()的类B中给定的实施?

Now, why is then x.foo() not invoking the foo() implementation provided by class A as well? Or, why is obj.foo() not invoking the implementation given for foo() in class B?

由于 X 的类型是可变的 IMyInterface的,因此富()的所提供方法的显式接口实现当调用B类对象优先 x.foo()。变量的 OBJ 的是不会类型的 IMyInterface的的的,因此, obj.foo()将不调用的富()的方法。

Because x is a variable of type IMyInterface, thus the explicit interface implementation of the foo() method provided by the object of type B takes precedence when invoking x.foo(). The variable obj is not of type IMyInterface, hence obj.foo() will not invoke the explicit interface implementation of the foo() method.

由于这些令人惊讶的结果,很容易理解为什么显式接口实现这已经由一个基类实现的接口确实是在大多数情况下是一个坏主意。俗话说:因为你可以并不意味着你应该

Because of these surprising results it is easy to understand why explicit interface implementations for interfaces which are already implemented by a base class is really a bad idea in most cases. As the saying goes: Just because you can does not mean you should.

这篇关于难道一个类继承类继承类/接口另一个类继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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