public Box( double length,double width,double height)
{
//注意this用法
this.Length = length;
this.Width = width;
this.Height = height;
}
Box box2 = new Box(3, 4, 5);
Box box3 = new Box(1, 2, 3);
如上所示,创建新的对象box3后,box2的值也会变成(1,2,3)。
原因是:在Box类里面,私有字段写成了static,相应的ToString方法也用了Box.
public override string ToString()
{
return String.Format("立方体的长:{0},宽:{1},高:{2}", Box.length, Box.width, Box.height);
}
修改后:
运行结果: