给定以下接口(interface):
interface IFoo
{
void Foo();
}
interface IBar : IFoo
{
void Bar();
}
指示给定类实现 IBar
的最佳方式是什么?
方法一:
class Implementation : IBar { ... }
这看起来更简单、更清晰,但它并不表示 Implementation
实现了 IFoo
。
方法 B:
class Implementation : IBar, IFoo { ... }
这似乎是最有用的信息,但额外的信息真的有必要吗?
最佳答案
就我个人而言,我会选择前者。对于后者,假设您处于这种情况,您有一堆派生类:
class DerivedA : Implementation, IBar, IFoo { ... }
class DerivedB : Implementation, IBar, IFoo { ... }
class DerivedC : Implementation, IBar, IFoo { ... }
class DerivedD : Implementation, IBar, IFoo { ... }
然后您对 Implementation
的要求发生变化,因此它现在需要实现另一个接口(interface),例如 IBaz
(它可以在不影响任何派生类的情况下实现只要它提供了一个非抽象的实现)那么你有两个选择:
保留所有派生类定义而不显式声明新接口(interface)信息,在这种情况下,根据标准,类定义现在是不正确的,如果人们希望始终遵循标准,则可能会产生误导。
找到所有派生类及其所有派生类,并修复它们的声明。这需要时间,容易出错且难以完全完成,并且会将您的单个文件更改变成多文件 checkin 。
这两个选项都没有吸引力,所以我会选择不会让您处于任何一种情况的选项。只需声明最派生的接口(interface)。
关于c# - 在 C# 中显示继承接口(interface)实现的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/478684/