粉丝.cs
using System;
namespace Fans {
class Fan {
private int speed;
private double radius;
private string color;
//default constructor
public Fan() {
this.speed = 1;
this.radius = 1.53;
this.color = "green";
}
//convenience constructor
public Fan(double newRadius) {
this.radius = newRadius;
}
public override string ToString() {
return "A " + radius + " inch " + color " fan at a speed of " + speed;
}
public int speed {
get { return speed; }
set { speed = newSpeed; }
}
public double radius {
get { return radius; }
set { radius = newRadius; }
}
public string color {
get { return color ; }
set { color = newColor; }
}
}
}
程序.cs
using System;
namespace Fans {
class Program {
static void Main(string[] args) {
Fan fan1 = new Fan();
fan1.speed = 3;
fan1.radius = 10.26;
fan1.color = "yellow";
Console.WriteLine(fan1);
}
}
}
当我尝试编译 Program.cs 时,出现两个相同的错误:
两个文件位于同一目录中。不知道还有什么问题。
这里有一些小问题需要修复。
ToString 中缺少 +
您需要使用集合中的值而不是 newRadius、newColor 和 newSpeed
您使用与变量相同的成员名称。在过去,我们会像下面的工作示例中那样使用下划线。不过考虑使用新格式 例如。公共速度{获取;放; }
使用系统;
命名空间粉丝 {
class Fan {
private int _speed;
private double _radius;
private string _color;
//default constructor
public Fan() {
this._speed = 1;
this._radius = 1.53;
this._color = "green";
}
//convenience constructor
public Fan(double newRadius) {
this._radius = newRadius;
}
public override string ToString() {
return "A " + _radius + " inch " + _color + " fan at a speed of " + _speed;
}
public int speed {
get { return _speed; }
set { _speed = value; }
}
public double radius {
get { return _radius; }
set { _radius = value; }
}
public string color {
get { return _color ; }
set { _color = value; }
}
}
}