返回多个接口的实现时匹配返回类型
如果单个函数中的对应接口实现需要每个接口,那么在返回多个接口的实现时,如何匹配返回类型?
这是我试图将其表达为代码的最佳方式:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过显式实现接口来做到这一点:
仅当 IAA 或 IBB 是接口而不是类时,这才有效,因为您不能从多个基类继承 IAABB 类。
You can do this by implementing interfaces explicitly:
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.