简体   繁体   English

当基类实现abtract时,在派生类上指定实现接口

[英]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继承自BaseBase继承自IFoo

So, there is no necessity for you to "Implement" both Base and IFoo when defining the class Derived . 因此,在定义Derived类时,没有必要“实现” BaseIFoo

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.

相关问题 派生自实现接口的基础 class - Derived from a base class that implements an interface 当我实现基类也实现的接口时,为什么不收到编译器警告? - Why don't I get a compiler warning when I implement an interface that a base class also implements? 实现接口的通用基类 - generic base class that implements interface 在基类和派生类中实现接口 - Implementing an interface in a base and derived class 强制派生类实现接口 - Force derived class to implement interface 实现一个泛型类,其中定义在基本接口类型上,但实现在从该基本接口派生的接口上? - Implement a generic class where the definition is on a base interface type but the implementation is on an interface derived from that base? 有没有一种方法可以强制派生类实现抽象类或嵌套在基类中的接口? - Is there a way to force a derived class to implement an abstract class or an interface nested in base class? 我是否需要在派生类的定义中指定由基类实现的接口? - Do I need to specify interface implemented by a base class in the definition of a derived class? c#有没有一种方法可以让我的基类和派生类分别实现Interface方法? - c# Is there a way that my base class and derived class can implement Interface methods separately? C#:当派生类及其基类都实现 IEnumerable 时,在派生迭代器类上使用 LINQ - C#: Using LINQ on a derived iterator class when both the derived class and its base implement IEnumerable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM