与需要在C#中访问它们的类成员和功能实现接口
我有此类:
public class Node
{
public int Data;
public Node Next;
}
这个界面:
public interface IMyList
{
Node Head { get; set; }
void Add(int elm); // add new element
void PrintAll(); //print all list
void Reverse(); // reverse list, implement without using helpers collection\arrays
}
我需要实现接口以使用所有内在函数
这是我的类实现接口的类:
public class MyCustomizedList : IMyList
{
Node IMyList.Head { get; set ; }
public void Add(int elm) { /*something goes here*/ } // add new element
public void PrintAll() { /*and here*/ } //print all list
public void Reverse() { /*and also here*/ }
// reverse list, implement without using helpers collection\arrays
}
但是我不明白我如何获得对类节点的字段的访问 例如,如果我想实现函数add() 如何将内部节点类的字段设置为将指针连接到下一个节点,而ELM值则将指针连接到数据?
I have this class:
public class Node
{
public int Data;
public Node Next;
}
and this interface:
public interface IMyList
{
Node Head { get; set; }
void Add(int elm); // add new element
void PrintAll(); //print all list
void Reverse(); // reverse list, implement without using helpers collection\arrays
}
and I need to implement the interface to use all it's inner functions
this is my class that implements the interface:
public class MyCustomizedList : IMyList
{
Node IMyList.Head { get; set ; }
public void Add(int elm) { /*something goes here*/ } // add new element
public void PrintAll() { /*and here*/ } //print all list
public void Reverse() { /*and also here*/ }
// reverse list, implement without using helpers collection\arrays
}
but I don't understand how do I gain access to the fields of class Node
for example If I want to Implement the function Add()
how do I set the fields of the inner Node Class to attach pointer to the Next Node and the elm value to it's data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现在,您正在实现属性
head
显式:要访问它,
。
的左手必须为ilist
。一种简单的方法是((iList)此).head
。例如,要将其打印到控制台:
或者,如果您没有理由实现
head
明确实现,则 not 。就像其他成员一样,隐式地实施它:Right now, you are implementing the property
Head
explicitly:To access it, the left hand side of the
.
must beIList
. One simple way to do that is((IList)this).Head
.For example, to print it to the console:
Alternatively, if there is no reason for you to implement
Head
explicitly, don't. Implement it implicitly instead, just like the other members:尝试一下:
我认为不是有效的数据结构。这堂课的目的是什么?
要添加一个项目,您必须迭代所有元素。
您没有任何删除方法。
头是公开的,也是其特性:不受外部错误的保护,没有被封装...
Try this:
I think isn't not an efficient data structure. What is the purpose of this class?
To Add an item you must iterate all elements.
You haven't any remove method.
Head is public and their properties too: is not protected from outside mistakes, is not encapsulated...