我试图从私人班级获得预先确定的输出(指定为颜色和速度)。但是,当我要求程序写入该行时,它会打印出Car_EX_3.Car
而不是颜色和数字。
这是我目前的代码
namespace Car_Ex_3
{
class Car
{
private string colour;
private double speed;
public Car(string colour)
{
this.colour = colour;
}
public Car(string colour, double speed)
{
this.colour = colour;
this.speed = speed;
}
public string GetColour()
{
return colour;
}
public double GetSpeed()
{
return speed;
}
class Program
{
static void Main(string[] args)
{
Car car1 = new Car("red" + " " + 50d);
Car car2 = new Car("Yellow" +" " + 60d);
Console.WriteLine(car1);
Console.WriteLine(car2);
Console.ReadKey();
}
}
}
}
答案 0 :(得分:4)
正如已经有很多人提到的那样:覆盖Object.ToString()
方法可以获得理想的结果。
C#中的每个类都派生自System.Object
类,但.ToString()
方法通常只打印实例类的名称。为防止这种情况,请在Car
类中实施一种新方法:
public override string ToString ()
{
return "Car (Color: " + colour + ", Speed: " + speed.ToString() + ")";
}
或String.Format
:
public override string ToString ()
{
return string.Format("Car (Color: {0}, Speed: {1})", colour, speed.ToString());
}
答案 1 :(得分:0)
你实际上做错了。
正确的方式:
Car car1 = new Car("red", 50d);
Car car2 = new Car("Yellow", 60d);
Console.WriteLine("Car1 Color: " + car1.colour + " Number: " + car1.speed);
Console.WriteLine("Car2 Color: " + car2.colour + " Number: " + car2.speed);
你必须使用带有2个参数的构造函数,一个参数颜色,第二个是速度。
在Console.WrtieLine()
中,您直接编写类对象,这就是为什么它会提供不需要的结果(Car_EX_3.Car
),因为它通过命名空间和类名告诉您对象的类型。
您必须访问colour
和speed
个对象的car1
和car2
属性。
最初你正在做Car car1 = new Car("red" + " " + 50d);
,它正在调用一个参数constrictor,而不是两个参数构造函数,因为"red" + " " + 50d
将由编译器转换为字符串。
正如上面的Ian H.所建议的那样,您可以覆盖ToString()
或使用自己的方法将复合类型(您的类对象)转换为您需要的字符串格式,并在代码中的任何位置使用。
答案 2 :(得分:0)
只需查看代码:
// Don't forget to include the access modifier
class Car
{
private string colour;
private double speed;
// Is it valid to have just a color and not a speed as well?
public Car(string colour)
{
this.colour = colour;
}
public Car(string colour, double speed)
{
this.colour = colour;
this.speed = speed;
}
// In C# this should be a property
public string GetColour()
{
return colour;
}
public double GetSpeed()
{
return speed;
}
class Program
{
static void Main(string[] args)
{
Car car1 = new Car("red" + " " + 50d);
Car car2 = new Car("Yellow" +" " + 60d);
This is incorrect
Console.WriteLine(car1);
Console.WriteLine(car2);
Console.ReadKey();
}
}
}
请尝试以下方法:
public class Car
{
private string colour;
private double speed;
// Is it valid to have just a color and not a speed as well?
public Car(string colour)
{
this.colour = colour;
// Initialize it to 0.0
speed = 0.0;
}
public Car(string colour, double speed)
{
this.colour = colour;
this.speed = speed;
}
// In C# this should be a property
public string Colour
{
get { return colour; }
}
public double Speed
{
get { return speed; }
}
class Program
{
static void Main(string[] args)
{
Car car1 = new Car("red" + " " + 50d);
Car car2 = new Car("Yellow" +" " + 60d);
// Do something like the following to print what you want
Console.WriteLine(car1.Colour + " " + car1.Speed);
Console.WriteLine(car2.Colour + " " + car2.Speed);
Console.ReadKey();
}
}
}
正如其他人提到的那样,你也可以通过覆盖ToString()方法来实现这一点。 Ian H.在他的答案中有一个很好的代码示例,如果那是你想要的路线。