Разработать класс Car. Добавить в класс 3 поля: марка автомобиля, цвет автомобиля и стоимость - C#

Узнай цену своей работы

Формулировка задачи:

Разработать класс Car. Добавить в класс 3 поля: марка автомобиля, цвет автомобиля (использовать перечисление), стоимость автомобиля (предусмотреть модификаторы доступа к полям). Добавить методы в класс, позволяющие изменять марку, цвет и стоимость автомобиля. Создать класс Truck, унаследовать методы от класса Car и переопределить по своему усмотрению.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Lab_3
{
    class Program
    {
        static void Main(string[] args)
        {
            Trukt t1 = new Trukt();
            Trukt t2 = new Trukt();
            t1.Marc = "Опель";
            t1.Color = "Чёрный";
            t1.Cost = 650000;
            t1.Pokaz();

            t2.Marc = "Ауди";
            t2.Color = "Белый";
            t2.Cost = 800000;
            t2.Pokaz();
            Console.WriteLine();
 
            Console.WriteLine("Итоговая комплектация:\n");
 
            t1.YellowColor();
            t1.Sum(20000);
            t1.Pokaz();

            t2.BlakColor();
            t2.Pokaz();

        }
        class Car
        {
            public string Marc { get; set; }
            public string Color { get; set; }
            public int Cost {get; set;}
            public void Pokaz()
            {
                Console.WriteLine("Данные машины: Марка:{0}, Цвет:{1}, Цена:{2}" , Marc,Color,Cost);
            }
        }
        class Trukt : Car
        {
            public void Sum(int money)//+money за доп. комплектацию
            {
                this.Cost += money;
            }
            public void BlakColor()
            {
                this.Color = "Чёрный";
            }
            public void YellowColor()
            {
                this.Color = "Жёлтый";
            }
        }
    }
}
У меня вопрос. Я правильно сделал или чего то не понял. Заранее спасибо.

Решение задачи: «Разработать класс Car. Добавить в класс 3 поля: марка автомобиля, цвет автомобиля и стоимость»

textual
Листинг программы
using System;
 
namespace Cars
{
    class Program
    {
        static void Main(string[] args)
        {
            Trukt t1 = new Trukt();
            Trukt t2 = new Trukt();
            t1.Marc = "Опель";
            t1.Color = Car.Colors.Black;
            t1.Cost = 650000;
            Console.WriteLine(t1.ToString());
 
 
            t2.Marc = "Ауди";
            t2.Color = Car.Colors.White;
            t2.Cost = 800000;
            Console.WriteLine("\n" + t2.ToString());
            Console.WriteLine();
 
            Console.WriteLine("Итоговая комплектация:\n");
 
            t1.SetColor(Car.Colors.Yellow);
            t1.Sum(20000);
            Console.WriteLine("\n" + t1.ToString());
 
 
            t2.SetColor(Car.Colors.Black);
            Console.WriteLine("\n" + t2.ToString());
            Console.ReadKey();
        }
    }
 
    class Car
    {
        public enum Colors { White, Black, Yellow }
 
        public string Marc { get; set; }
        public Colors Color { get; set; }
        public int Cost { get; set; }
 
        public Car(string marc, Colors color, int cost)
        {
            Marc = marc;
            Color = color;
            Cost = cost;
        }
 
        public Car()
        {
        }
 
        public override string ToString()
        {
            return string.Format("Марка:{0} \nЦвет:{1} \nЦена:{2}", Marc, Color, Cost);
        }
    }
 
    class Trukt : Car
    {
        public void Sum(int money)//+money за доп. комплектацию
        {
            this.Cost += money;
        }
 
        public void SetColor(Colors color)
        {
                switch ((int)color)
                {
                    case 0: this.Color = Car.Colors.White; break;
                    case 1: this.Color = Car.Colors.Black; ; break;
                    case 2: this.Color = Car.Colors.Yellow; ; break;
                }
        }
    }
}

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 3.455 из 5
Похожие ответы