C# 当:base()存在时,如何使用构造函数链接?

C# 当:base()存在时,如何使用构造函数链接?,c#,oop,c#-4.0,C#,Oop,C# 4.0,Square上的构造函数需要设置自己的参数,该参数继承自父类Polygon class Polygon { public int NumSides; public Polygon(int numSides) { NumSides = numSides; } } class Square : Polygon { public int SideLength; public Square (int sideLength) :

Square上的构造函数需要设置自己的参数,该参数继承自父类Polygon

class Polygon
{
    public int NumSides;

    public Polygon(int numSides)
    {
        NumSides = numSides;
    }
}

class Square : Polygon 
{

    public int SideLength; 

    public Square (int sideLength) : this(numSides) : base(numSides)
    {
        SideLength = sidelength; 
        NumSides = NumSides; 
    }
}

要传递给基类的参数必须是子类的ctor的一部分:

class Square : Polygon {

    public int SideLength; 

    public Square (int sideLength, int numSides) : base(numSides) {
        SideLength = sidelength; 
        // NumSides will be handled by base class ctor
    }

    // ctor overload with fixed number of sides for squares
    public Square (int sideLength) : this(sideLength, 4) {
    }
}

NumSides=NumSides这是什么意思?你想知道应该如何调用基本构造函数吗?NumSides=边数。什么是
:这(NumSides)
是在
正方形中执行的?在该位置使用
通常用于调用不同的构造函数-但这似乎是试图调用同一个构造函数,即使它工作,也会产生堆栈溢出。您的问题不清楚。类Square的构造函数不正确
this(numSides)
一次又一次地调用它自己,并且永远不会跳出那个循环。我将进入无限循环并使用StackOverFlowException中断。
class Square : Polygon {

    public int SideLength; 

    public Square (int sideLength, int numSides) : base(numSides) {
        SideLength = sidelength; 
        // NumSides will be handled by base class ctor
    }

    // ctor overload with fixed number of sides for squares
    public Square (int sideLength) : this(sideLength, 4) {
    }
}