与需要在C#中访问它们的类成员和功能实现接口

发布于 2025-02-03 23:45:33 字数 872 浏览 6 评论 0原文

我有此类:

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 技术交流群。

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

发布评论

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

评论(2

晚风撩人 2025-02-10 23:45:33

现在,您正在实现属性head 显式

Node IMyList.Head { get; set ; }

要访问它,的左手必须为ilist。一种简单的方法是((iList)此).head

例如,要将其打印到控制台:

Console.WriteLine(((IList)this)Head);

或者,如果您没有理由实现head明确实现,则 not 。就像其他成员一样,隐式地实施它:

public Node Head { get; set; }

Right now, you are implementing the property Head explicitly:

Node IMyList.Head { get; set ; }

To access it, the left hand side of the . must be IList. One simple way to do that is ((IList)this).Head.

For example, to print it to the console:

Console.WriteLine(((IList)this)Head);

Alternatively, if there is no reason for you to implement Head explicitly, don't. Implement it implicitly instead, just like the other members:

public Node Head { get; set; }
寂寞陪衬 2025-02-10 23:45:33

尝试一下:

public class MyCustomizedList : IMyList
{
    public Node Head { get; set; }

    public void Add(int elm)
    {
        var newNode = new Node { Data = elm };
        if (this.Head == null)
        {
            this.Head = newNode;
        }
        else
        {
            var node = this.Head;
            while (node.Next != null)
            {
                node = node.Next;
            }

            node.Next = newNode;
        }
    }

    public void PrintAll()
    {
        var builder = new StringBuilder();

        var node = this.Head;
        while (node.Next != null)
        {
            builder.AppendLine(node.Data.ToString());
            node = node.Next;
        }

        var text = builder.ToString();
        Console.WriteLine(text);
    }

    public void Reverse()
    {
        if (this.Head?.Next != null)
        {
            var node = this.Head;
            this.Head = Reverse(node, node.Next);
            node.Next = null;
        }
    }

    private static Node Reverse(Node node1, Node node2)
    {
        if (node2.Next != null)
        {
            var lastNode = Reverse(node2, node2.Next);
            node2.Next = node1;
            return lastNode;
        }

        node2.Next = node1;
        return node2;
    }
}

我认为不是有效的数据结构。这堂课的目的是什么?
要添加一个项目,您必须迭代所有元素。
您没有任何删除方法。
头是公开的,也是其特性:不受外部错误的保护,没有被封装...

Try this:

public class MyCustomizedList : IMyList
{
    public Node Head { get; set; }

    public void Add(int elm)
    {
        var newNode = new Node { Data = elm };
        if (this.Head == null)
        {
            this.Head = newNode;
        }
        else
        {
            var node = this.Head;
            while (node.Next != null)
            {
                node = node.Next;
            }

            node.Next = newNode;
        }
    }

    public void PrintAll()
    {
        var builder = new StringBuilder();

        var node = this.Head;
        while (node.Next != null)
        {
            builder.AppendLine(node.Data.ToString());
            node = node.Next;
        }

        var text = builder.ToString();
        Console.WriteLine(text);
    }

    public void Reverse()
    {
        if (this.Head?.Next != null)
        {
            var node = this.Head;
            this.Head = Reverse(node, node.Next);
            node.Next = null;
        }
    }

    private static Node Reverse(Node node1, Node node2)
    {
        if (node2.Next != null)
        {
            var lastNode = Reverse(node2, node2.Next);
            node2.Next = node1;
            return lastNode;
        }

        node2.Next = node1;
        return node2;
    }
}

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...

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