[英]Specify implement interface on derived class when base class implements it abtract
I was wondering if there are any (runtime) difference if I implement an interface on a derived class if the base abstract class implements it already abstract: 如果我在派生类上实现一个接口,如果基本抽象类实现它已经抽象,我想知道是否存在任何(运行时)差异:
public interface IFoo
{
void Bar();
}
public abstract class Base : IFoo
{
public abstract void Bar();
}
public class Derived : Base, IFoo // does this make any difference?
{
public override void Bar()
{
// do something
}
}
Is there any difference writing the "implemention" IFoo
on Derived
for example when I check if an instance implements an interface at runtime etc.? 在Derived
上编写“实现” IFoo
是否有任何区别,例如当我检查实例是否在运行时实现接口等时?
Consider following on your example. 考虑按照你的例子。 Must the following be possible? 必须满足以下条件吗?
void f(Base b){
IFoo ifoo = b;
}
Because an object b is a Base, it must be an IFoo because Base implements IFoo. 因为对象b是Base,所以它必须是IFoo,因为Base实现了IFoo。 Now consider the following. 现在考虑以下内容。
var d = new Derived();
f(d);
Since d is a Derived, it is a Base, because derived inherits from Base. 由于d是Derived,因此它是Base,因为派生继承自Base。 Because of this, we can pass it to f, where it is then assigned to an IFoo, as we did before. 因此,我们可以将它传递给f,然后将其分配给IFoo,就像我们之前所做的那样。
Essentially, because a derived class still is also all of its base classes, it already implements all of its base classes' interfaces. 本质上,因为派生类仍然是它的所有基类,所以它已经实现了它的所有基类的接口。 The designers of the language acknowledged this and there is no need to redeclare that a derived class is implementing interfaces that are implemented by its base classes. 该语言的设计者承认这一点,并且没有必要重新声明派生类正在实现由其基类实现的接口。
I guess that maybe your question arises because of where you actually put the method body, but that really isn't relevant. 我想也许你的问题出现了,因为你实际放置了方法体,但这确实是不相关的。 The declaration of the interface is a contract for other consumers of objects of that type. 接口的声明是该类型对象的其他使用者的合同。 Those consumers don't care where the method body was defined, all that they care is that when they have an IFoo, that it has a method with the signature void Bar()
that can be called, as I showed earlier, inheritance must include all interfaces, so there is no need to declare them again. 那些消费者并不关心定义方法体的位置,他们关心的只是当他们有一个IFoo时,它有一个带有可以被调用的签名void Bar()
的方法,正如我之前所说,继承必须包括所有接口,所以不需要再次声明它们。
The answer is No . 答案是否定的 。 There is no difference. 没有区别。
The Class Derived
inherits from Base
and Base
in turn inherits from IFoo
. Class Derived
继承自Base
和Base
继承自IFoo
。
So, there is no necessity for you to "Implement" both Base
and IFoo
when defining the class Derived
. 因此,在定义Derived
类时,没有必要“实现” Base
和IFoo
。
The hierarchy is established when you are defining the class Base
: 在定义类Base
时建立层次结构:
public abstract class Base : IFoo
and continued when you define Derived
as below: 并在您定义Derived
时继续如下:
public abstract class Derived : Base
The only problem may arise when you "Remove" Base
from the hierarchy by redefining the Base
class by removing the : IFoo
implementation from the Base
class definition as below: 通过从Base
类定义中删除: IFoo
实现重新定义Base
类,从层次结构中“删除” Base
可能会出现唯一的问题,如下所示:
public abstract class Base
The bottom line is: , IFoo
is redundant and is therefore not necessary. 底线是: , IFoo
是多余的,因此不是必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.