为什么合约类不能继承?

发布于 2024-10-18 06:59:24 字数 571 浏览 2 评论 0原文

如果我有一个接口 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煮酒 2024-10-25 06:59:24

这是我的猜测(我应该在发布之前想到) - 如果接口 B 扩展了多个接口 - 比如说 AC,并附带合同类 AContractCContract - BContract 应该继承哪个合约类?

总是删除基本接口,而不是混合存根和契约类的继承,这样会减少歧义。

Here's my guess (which I should have thought of before posting) - If interface B extended multiple interfaces - say A and C, with attendant contract classes AContract and CContract - which contract class should BContract inherit from?

There's less ambiguity in always stubbing out base interfaces, rather than mixing up stubs and inheritance of contract classes.

层林尽染 2024-10-25 06:59:24

它是受支持的,但不是以您想象的方式:如果接口 B 派生自接口 A,则在 A 上定义的所有合约也自动B定义。

编辑:这是评论的示例;

[ContractClass(typeof(IAContracts))]
interface IA
{
    void Foo(int x);
}

[ContractClass(typeof(IBContracts))]
interface IB : IA
{
    void Bar(int y);
}

[ContractClassFor(typeof(IA))]
abstract class IAContracts : IA
{
    public void Foo(int x)
    {
        Contract.Requires(x >= 0);
    }
}

[ContractClassFor(typeof(IB))]
abstract class IBContracts : IB
{
    // inherited from IA
    public abstract void Foo(int x);

    // declared in IB
    public void Bar(int y)
    {
        Contract.Requires(y >= 0);
    }
}

It is supported, but not in the way you're thinking: if interface B derives from interface A, then all the contracts defined on A are also automatically defined for B.

Edit: Here's an example for the comment;

[ContractClass(typeof(IAContracts))]
interface IA
{
    void Foo(int x);
}

[ContractClass(typeof(IBContracts))]
interface IB : IA
{
    void Bar(int y);
}

[ContractClassFor(typeof(IA))]
abstract class IAContracts : IA
{
    public void Foo(int x)
    {
        Contract.Requires(x >= 0);
    }
}

[ContractClassFor(typeof(IB))]
abstract class IBContracts : IB
{
    // inherited from IA
    public abstract void Foo(int x);

    // declared in IB
    public void Bar(int y)
    {
        Contract.Requires(y >= 0);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文