首页 新闻 会员 周边

请来试试看一个c#泛型问题

0
[已关闭问题]
现有接口<br><div class="cnblogs_code"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span id="Codehighlighter1_31_121_Open_Text"></span><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">interface</span>&nbsp;ITreeNode<br><div class="cnblogs_code"><span style="color: rgb(0, 0, 0);">{<br>&nbsp;&nbsp;&nbsp;ITreeNode&nbsp;Parent&nbsp;{&nbsp;</span><span style="color: rgb(0, 0, 255);">get</span><span style="color: rgb(0, 0, 0);">;&nbsp;}<br>&nbsp;&nbsp;&nbsp;ICollection</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">ITreeNode</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;Children&nbsp;{&nbsp;</span><span style="color: rgb(0, 0, 255);">get</span><span style="color: rgb(0, 0, 0);">;&nbsp;}<br>}<br></span></div></div><br>实现类<br><div class="cnblogs_code"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;Node&nbsp;:&nbsp;ITreeNode<br>{<br>&nbsp;&nbsp;&nbsp;List</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">Node</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;nodes;<br>&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;ICollection</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">ITreeNode</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;Children<br>&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">get</span><span style="color: rgb(0, 0, 0);">&nbsp;{&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;nodes;&nbsp;}<br>&nbsp;&nbsp;&nbsp;}<br>}</span></div>会出现编译错误。<br><br>无法使用<br><span style="color: rgb(0, 0, 0);">List</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">Node</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;nodes; =&gt; </span><span style="color: rgb(0, 0, 0);">List</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">ITreeNode</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;nodes;</span><br>如何解决?<br>
dragonpig的主页 dragonpig | 初学一级 | 园豆:200
提问于:2008-08-08 15:23
< >
分享
其他回答(2)
0
e....ITreeNode 和 Node 是什么关系? 返回的泛型类型可能不一致的原因。。
Allie | 园豆:707 (小虾三级) | 2008-08-08 17:17
0
public class Node : ITreeNode { List&lt;ITreeNode&gt; nodes; public ICollection<ITreeNode> Children { get { return nodes; } } }
deerchao | 园豆:8367 (大侠五级) | 2008-08-09 01:55
0
应该是: public class Node:ITreeNode { Node node; public ITreeNode Parent { get { return node; } } List<ITreeNode> nodes; public ICollection<ITreeNode> Children { get { return nodes; } } } 接口里面有两个字段,类里面只实现了一个!List<ITreeNode> nodes;不能写成List<Node> nodes,好像里氏替换,不能换<>里面的... 不过当你Children[i]获取对象时,可以遵循里氏替换Node node = Children[0];这么写!
Jerry Young | 园豆:435 (菜鸟二级) | 2008-08-09 22:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册