返回多个接口的实现时匹配返回类型

发布于 2025-01-11 20:14:49 字数 372 浏览 4 评论 0原文

如果单个函数中的对应接口实现需要每个接口,那么在返回多个接口的实现时,如何匹配返回类型?

这是我试图将其表达为代码的最佳方式:

interface IA
{
    IAA XYZ();
}

interface IAA
{

}

interface IB
{
    IBB XYZ();
}

interface IBB
{

}

class IAABB : IAA, IBB
{

}

class T : IA, IB
{
    public IAABB XYZ() { return null; }
}

上面的代码导致“T.XYZ() 无法实现 IA.XYZ(),因为它没有匹配的 IAA 返回类型”。当然,IB.XYZ() 也是如此。

How can I match return types when returning an implementation of multiple interfaces if each of those interfaces is required by counterpart interface implementations in a single function?

That's my best shot at trying to put this into words, onto the code:

interface IA
{
    IAA XYZ();
}

interface IAA
{

}

interface IB
{
    IBB XYZ();
}

interface IBB
{

}

class IAABB : IAA, IBB
{

}

class T : IA, IB
{
    public IAABB XYZ() { return null; }
}

The code above results in "T.XYZ() cannot implement IA.XYZ() because it does not have the matching return type of IAA". Same for IB.XYZ(), of course.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

浮世清欢 2025-01-18 20:14:49

您可以通过显式实现接口来做到这一点:

class T :IA,IB{
    IAA IA.XYZ() { return XYZ(); }
    IBB IB.XYZ() { return XYZ(); }
    public IAABB XYZ() { return null; }

仅当 IAA 或 IBB 是接口而不是类时,这才有效,因为您不能从多个基类继承 IAABB 类。

You can do this by implementing interfaces explicitly:

class T :IA,IB{
    IAA IA.XYZ() { return XYZ(); }
    IBB IB.XYZ() { return XYZ(); }
    public IAABB XYZ() { return null; }

this will work only if IAA or IBB is an interface and not classes because you cannot inherit class IAABB from more than one base class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文