Recursive class C#
本文关键字:class Recursive | 更新日期: 2023-09-27 17:58:53
我可以这样定义构造函数吗?附加问题:我可以在构造函数中调用类的构造函数吗?
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);
}
}