中易网

用C#编写出一个通用的车类( Auto),该类具有车牌(Name),价格(Price))、颜色(c

答案:1  悬赏:30  
解决时间 2021-01-13 14:55
  • 提问者网友:骨子里的高雅
  • 2021-01-12 19:12
用C#编写出一个通用的车类( Auto),该类具有车牌(Name),价格(Price))、颜色(c
最佳答案
  • 二级知识专家网友:走死在岁月里
  • 2021-01-12 19:30
您好,请参考下面的代码:
class Auto
    {
        public string Name { set; get; }
        public double Price { set; get; }
        public string Color { set; get; }
    }
    class Car : Auto
    {
        List list = new List();
        public Car()
        {
            
        }
        public Car(string name, double price, string color)
        {
            this.Name = name;
            this.Price = price;
            this.Color = color;
        }
        public void AddCar(Car c)
        {
            list.Add(c);
        }
        public double GetAveragePrice()
        {
            return list.Sum(d => d.Price) / list.Count;
        }
    }测试:
 static void Main(string[] args)
        {
            Car car = new Car();

            for (int i = 0; i < 5; i++)
            {
                Car c1 = new Car();
                c1.Name = "name" + i;
                c1.Price = i + 10;
                c1.Color = "red";

                car.AddCar(c1);
            }

            Console.WriteLine(car.GetAveragePrice());

            

        }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息