简体   繁体   English

在 C# 中使用需要访问它们的类成员和函数实现接口

[英]Implementing interface with class members and functions that need access to them in C#

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?但我不明白如何访问类节点的字段,例如如果我想实现函数 Add() 我如何设置内部节点类的字段以将指针附加到下一个节点和榆树对它的数据有价值吗?

Right now, you are implementing the property Head explicitly :现在,您正在显式实现属性Head

Node IMyList.Head { get; set ; }

To access it, the left hand side of the .要访问它,请在. must be IList .必须是IList One simple way to do that is ((IList)this).Head .一种简单的方法是((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 .或者,如果您没有理由明确实施Head请不要. Implement it implicitly instead, just like the other members:而是隐式实现它,就像其他成员一样:

public Node Head { get; set; }

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...头部是公共的,它们的属性也是:不受外部错误的保护,没有被封装......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM