私有密封类节点 [英] private sealed class Node

查看:81
本文介绍了私有密封类节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个与Node类有关的问题.在我的大学里,我们有一个如何描述Node类的示例, 它看起来像这样:私有密封类Node",但是我想弄清楚为什么Node类必须在链接列表类中,因为否则,如果我将Node类与List类分开声明的话,Visual Studio会给我一个提示.错误的 我无法在名称空间内声明私有类.所以我的问题是,为什么Node类应该在列表类内部?  

Good day to everyone, I've got a question related to Node class. In my university we have an example of how should Node class be described, it looks like this : "private sealed class Node", but I'cant figure out why Node class has to be inside linked list class, because otherwise if I would declare Node class separately from  list class visual studio would give me an error, that I can not declare private  class inside the namespace. So my question is , why does Node class should be inside list class ?  

推荐答案

肯定还有其他事情在您的问题文本中不明显.通常,将Node类声明为List之外,以便外部代码可以创建Node的实例,然后将Node添加到列表中.如果你的 代码不是那样组织的,必须有其他一些设计因素或准则导致采用不同的方式来做事.

There must be something else going on that is not obvious from the text of your question. Normally, the Node class would be declared OUTSIDE the List, so that the external code can create an instance of a Node, and then add the Node to the list. If your code is not organized like that, there must be some other design factors or criteria that lead to doing things in a different way.

示例:

namespace Test
{
  using System.Collections.Generic;

  class Example
  {
     class Node
    {
        public string SomeData { get; set; }
    }

    public static void Main()
    {
        List<Node> theList = new List<Node>();

        Node myNode = new Node { SomeData="Example" };
        theList.Add(myNode);
        // ....
    }
  }
}


这篇关于私有密封类节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆