一个构造函数的问题
class Class1
{
[STAThread]
static void Main(string[] args)
{
Rectangle rect = new Rectangle(0, 0, 20, 50);
Square sqr = new Square(0, 0, 20, 50);
rect.ShowArea();
sqr.ShowArea();
Console.WriteLine( "\n\n按回车键退出 ");
Console.ReadLine();
}
}
class Rectangle
{
public int left, top, width, height;
protected string type;
public Rectangle(int aLeft,int aTop, int aWidth,int aHeight)
{
left = aLeft;
top = aTop;
width = aWidth;
height = aHeight;
type = "矩形 ";
}
public void ShowArea()
{
Console.WriteLine( "当前形状为{0}, 面积是{1} ",
type, width*height);
}
}
class Square : Rectangle
{
public Square(int aLeft,int aTop,int aWidth
):base(0,0,0,0)
{
type = "正方形 ";
}
public Square(int aLeft,int aTop,int aWidth,int aHeight):this()
{
left = aLeft;
top = aTop;
if (aWidth > aHeight)
{
width = aHeight;
height = aHeight;
}
else
{
width = aWidth;
height = aWidth;
}
}
}
其中有一个构造函数:public Square(int aLeft,int aTop,int aWidth
):base(0,0,0,0)
{
type = "正方形 ";
}
可是这样会报错,要改成这样:public Square():base(0,0,0,0)
{
type = "正方形 ";
}
程序才不会报错.请问这是为什么?为什么要这样改?
[解决办法]
public Square(int aLeft,int aTop,int aWidth,int aHeight):this()
你这里的:this()调用的是哪个构造函数?!!