为什么合约类不能继承?
如果我有一个接口 A
和一个扩展 A
的接口 B
,我可以创建一个继承的合约类 BContract
合同
?
如果我尝试这样做,我会收到以下警告:
(1,1):警告 CC1066:CodeContracts:类“BContract”被注释为接口“B”的契约,并且不能具有除 System.Object 之外的显式基类。
我还没有对此进行测试,但我假设 BContract
中的 A
接口的实现被忽略,并且 AContract
中指定的合约优先 - 换句话说,我可以从 A
中删除所有方法和属性。
我知道这样做并不是一个很大的不便,但对于任何查看 BContract
的人来说,它似乎缺乏清晰度。能够从 AContract
派生 BContract
会节省时间、空间和理解,那么为什么不支持这一点呢?
If I have an interface A
and interface B
that extends A
, can I create a contract class BContract
that inherits AContract
?
If I try this, I get the following warning:
(1,1): warning CC1066: CodeContracts: Class 'BContract' is annotated as being the contract for the interface 'B' and cannot have an explicit base class other than System.Object.
I haven't tested this yet, but I assume that the implementation of the A
interface in BContract
is ignored, and the contracts specified in AContract
take precedence - in other words I can stub out all methods and properties from A
.
I appreciate that it's not a massive inconvenience to do this, but it does seem to lack clarity for anyone looking at BContract
. It would save time, space and understanding to be able to derive BContract
from AContract
, so why is this not supported?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的猜测(我应该在发布之前想到) - 如果接口
B
扩展了多个接口 - 比如说A
和C
,并附带合同类AContract
和CContract
-BContract
应该继承哪个合约类?总是删除基本接口,而不是混合存根和契约类的继承,这样会减少歧义。
Here's my guess (which I should have thought of before posting) - If interface
B
extended multiple interfaces - sayA
andC
, with attendant contract classesAContract
andCContract
- which contract class shouldBContract
inherit from?There's less ambiguity in always stubbing out base interfaces, rather than mixing up stubs and inheritance of contract classes.
它是受支持的,但不是以您想象的方式:如果接口
B
派生自接口A
,则在A
上定义的所有合约也自动为B
定义。编辑:这是评论的示例;
It is supported, but not in the way you're thinking: if interface
B
derives from interfaceA
, then all the contracts defined onA
are also automatically defined forB
.Edit: Here's an example for the comment;