如何实现定义另一个接口元素的接口
Interface A
{
string x {get;set;}
IEnumarable<InterfaceB> DetailList { get; set; }
}
Interface B
{
int z;
int y;
}
Class B :Interface B
{
implements z;
implements y;
}
Class A :Interface A
{
implements x;
IEnumarable<ClassB> DetailList {get;set;} // This line is giving trouble.
}
这段代码违反了OO概念吗?我想如果我从 InterfaceB 派生 ClassB 那么我可以在 ClassA 中使用 ClassB 而不是 InterfaceB。 VS不喜欢这个,它要求我在ClassA中使用InterfaceB而不是ClassB。 还有其他方法可以做到这一点吗?
我愿意考虑替代设计选项,我有一些域对象,其属性由接口 A 定义,每个域对象都有由接口 B 定义的相应对象 例如 演唱会(A) 演唱会地点(B) 喜剧表演(A) 喜剧表演地点(B)
如果您认为我不够清楚,请随时提出更多问题。
提前致谢
Interface A
{
string x {get;set;}
IEnumarable<InterfaceB> DetailList { get; set; }
}
Interface B
{
int z;
int y;
}
Class B :Interface B
{
implements z;
implements y;
}
Class A :Interface A
{
implements x;
IEnumarable<ClassB> DetailList {get;set;} // This line is giving trouble.
}
Is this code violating OO concept. I thought if I derive ClassB from InterfaceB then I can use ClassB in my ClassA instead of InterfaceB. VS is not liking this, Its asking me to use InterfaceB instead of ClassB in ClassA.
Is there any other way to go about doing this.
I am willing to consider alternate designing options, I have some domain objects whose properties are defined by Interface A and each domain object would have corresponding object defined by interface B
e.g
concert(A) concertlocations(B)
comedyshow(A) comedyshowlocations(B)
Feel free to ask more questions if you think I am not being clear enough.
Thanks in Advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以这样:
但我不确定这对你来说是否合适?
请参阅此处了解更多信息: c# 接口实现 - 为什么这不构建?
You can this:
but I'm not sure this is desirable for you?
see here for more info: c# interface implemention - why does this not build?
接口 A 只是表示
IEnumarable详细列表{获取;放; }
必须存在于任何实现它的类中,而InterfaceB
恰好是 IEnumerable 的泛型类型,而不是它的实现之一。类 B 不如接口 B 通用,因此它不允许您以这种方式使用它是完全合乎逻辑的。
Interface A just says that the
IEnumarable<InterfaceB> DetailList { get; set; }
has to be present in any class, that is implementing it, with exactlyInterfaceB
being the generic type for IEnumerable, and not one of it's implementaions.Class B is less general than Interface B, therefore it is completely logical that it does not allow you to use it in such manner.