如果汽车没有颜色的时候默认值为黑色怎么写 在构造方法里面写这个
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// 指定了颜色参数的构造函数
var car1 = new Car("奥迪Q5L", "奥迪", 456800, Color.Gray);
Console.WriteLine(car1.ToString());
// 没有指定颜色参数的构造函数
var car2 = new Car("沃尔沃XC60新能源", "沃尔沃", 425600);
Console.WriteLine(car2.ToString());
}
}
public enum Color
{
Black,
White,
Gray,
GrayBlack
}
public class Car
{
public Car(string name,string brand, decimal price, Color color=Color.Black)
{
Name = name;
Brand = brand;
Color = color;
Price = price;
}
public string Name { get; set; }
public string Brand { get; set; }
public Color Color { get; set; }
public decimal Price { get; set; }
/// <summary>Returns a string that represents the current object.</summary>
/// <returns>A string that represents the current object.</returns>
public override string ToString()
{
return $"洗车名称:{Name},品牌:{Brand},颜色:{Color},价格:{Price}";
}
}
}
运行结果:
洗车名称:奥迪Q5L,品牌:奥迪,颜色:Gray,价格:456800
洗车名称:沃尔沃XC60新能源,品牌:沃尔沃,颜色:Black,价格:425600
public class car{
public string color{set;get;}="黑色";
}