Загрузка данных
public class Car
{
// Поля класса
public string make;
public string model;
public int year;
public string color;
public int mileage;
// Существующий конструктор (предполагается из предыдущих шагов)
public Car(string make, string model, int year, string color, int mileage)
{
this.make = make;
this.model = model;
this.year = year;
this.color = color;
this.mileage = mileage;
}
// Перегруженный конструктор, который принимает только марку и модель
public Car(string make, string model)
{
this.make = make;
this.model = model;
// Установка значений по умолчанию
this.year = DateTime.Now.Year; // Текущий год
this.color = "Unknown"; // По умолчанию "Unknown"
this.mileage = 0; // По умолчанию 0
}
public string DisplayInfo()
{
return $"Автомобиль: {make} {model}, Год: {year}, Цвет: {color}, Пробег: {mileage}";
}
}
// Использование в основной программе:
Car newCar = new Car("Honda", "Accord");
Console.WriteLine(newCar.DisplayInfo());
public class Car
{
public int performance = 100; // Базовая производительность
// Метод с необязательным параметром
public void TuneUp(int performanceIncrease = 10)
{
// Увеличиваем производительность на указанный процент
performance += performanceIncrease;
}
}
// Использование:
Car car = new Car();
car.TuneUp(15); // Увеличит на 15%
car.TuneUp(); // Увеличит на 10% (значение по умолчанию)
using System;
class Calculator
{
// Метод сложения двух целых чисел
public int Add(int a, int b)
{
return a + b;
}
// Метод сложения двух чисел с плавающей точкой
public double Add(double a, double b)
{
return a + b;
}
// Метод сложения трех целых чисел
public int Add(int a, int b, int c)
{
return a + b + c;
}
}
class Program
{
static void Main()
{
Calculator calculator = new Calculator();
int sum1 = calculator.Add(5, 10);
double sum2 = calculator.Add(3.5, 2.5);
int sum3 = calculator.Add(2, 4, 6);
Console.WriteLine("Sum of integers: " + sum1);
Console.WriteLine("Sum of doubles: " + sum2);
Console.WriteLine("Sum of three integers: " + sum3);
}
}
using System;
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Course { get; set; }
// Конструктор по умолчанию
public Student() { }
// Конструктор с параметрами name и age
public Student(string name, int age)
{
Name = name;
Age = age;
}
// Конструктор с именем, возрастом и курсом
public Student(string name, int age, string course)
{
Name = name;
Age = age;
Course = course;
}
}
class Program
{
static void Main()
{
Student student1 = new Student();
Student student2 = new Student("John", 20);
Student student3 = new Student("Alice", 22, "Computer Science");
Console.WriteLine(student1.Name); // Будет пусто
Console.WriteLine(student2.Name); // Выведет John
Console.WriteLine(student3.Name); // Выведет Alice
}
}
using System;
class Printer
{
// Метод с одним строковым параметром
public void Print(string message)
{
Console.WriteLine(message);
}
// Метод со строкой и необязательным параметром ConsoleColor
public void Print(string message, ConsoleColor color = ConsoleColor.White)
{
ConsoleColor currentColor = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ForegroundColor = currentColor; // Возвращаем исходный цвет
}
}
public class Program
{
static void Main()
{
Printer printer = new Printer();
// Вызов метода без указания цвета (белый по умолчанию)
printer.Print("Hello, world!");
// Вызов с указанием желтого цвета
printer.Print("Welcome to the program!", ConsoleColor.Yellow);
}
}