繁体   English   中英

递归类C#

[英]Recursive class C#

我可以这样定义构造函数吗? 附加问题:我可以在本身的构造函数中调用类的构造函数吗?

class SubArray
{
    List<int> array;
    string parent;
    string name;
    SubArray child;

    public SubArray(SubArray child, string name)
    {
        this.child = child;
        List<int> array = new List<int>();
        this.name = name;
    }
}

对此没有限制,但是像任何递归一样,它需要一个停止条件。 否则会导致堆栈溢出(PUN意图:))。

我想你可以做这样的事情,并且没有....明显的问题:

public SubArray(SubArray child, string name)
{
    this.child = child;
    this.array = new List<int>();
    this.name = name;

    if (child != null && child.child != null)
    {
        this.child.child = new SubArray(child.child,name);
    }
}

暂无
暂无

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

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