Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Завдання: Інкапсуляція | Принципи ООП
C# Понад Базовий Рівень

bookЗавдання: Інкапсуляція

Базовий код містить два класи: Vehicle та Car. Деякі поля потрібно приховати, а деякі — зробити доступними.

  • Відкоригувати видимість полів type та modelYear у класі Vehicle так, щоб вони були недоступні ззовні класу, включаючи похідні класи;
  • Створити новий метод під назвою getModelYear у класі Vehicle, який повертає значення modelYear. Цей метод має бути доступним звідусіль. Це дозволить користувачам класу отримувати значення modelYear, але не змінювати його ззовні;
  • Властивості ownerName та fuel мають бути недоступними з будь-якого місця;
  • Створити метод під назвою getFuel, який повертає значення fuel;
  • Створити метод під назвою addFuel, який приймає аргумент типу float з іменем fuel. Усередині методу додати значення fuel до властивості fuel (this.fuel).
index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
using System; class Vehicle { // Edit code below this line public string type; public int modelYear; // Edit code above this line // Create a method below this line // Create a method above this line public Vehicle(string type, int modelYear) { this.type = type; this.modelYear = modelYear; } } class Car : Vehicle { // Edit code below this line public string brandName; public string numberPlate; public string ownerName; public float fuel; // Edit code above this line // Create a method below this line // Create a method above this line public Car(int modelYear, string brandName, string numberPlate, string ownerName, float fuel) : base("Car", modelYear) { this.brandName = brandName; this.numberPlate = numberPlate; this.ownerName = ownerName; this.fuel = fuel; } } class Program { static void Main() { // Create an instance of Car Car myCar = new Car(2022, "Toyota", "ABC123", "John Doe", 50.0f); // Accessing properties and methods from Car class Console.WriteLine($"Brand: {myCar.brandName}"); Console.WriteLine($"Number Plate: {myCar.numberPlate}"); // Accessing getModelYear method from Vehicle class Console.WriteLine($"Model Year: {myCar.getModelYear()}"); // Accessing getFuel method from Car class Console.WriteLine($"Fuel: {myCar.getFuel()}"); // Adding fuel using addFuel method myCar.addFuel(10.0f); Console.WriteLine($"After adding fuel, new Fuel: {myCar.getFuel()}"); } }
  1. Методи getModelYear та getFuel є методами-геттерами. Вони не приймають аргументів, є public і лише повертають значення.
  2. Метод addFuel є методом-сеттером. Він є public і приймає один аргумент типу float з іменем fuel. Для оновлення значення пального, можливо, потрібно використати ключове слово this. (this.fuel += fuel)
index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
using System; class Vehicle { // Edit code below this line private string type; private int modelYear; // Edit code above this line // Create a method below this line public int getModelYear() { return modelYear; } // Create a method above this line public Vehicle(string type, int modelYear) { this.type = type; this.modelYear = modelYear; } } class Car : Vehicle { // Edit code below this line public string brandName; public string numberPlate; private string ownerName; private float fuel; // Edit code above this line // Create a method below this line public float getFuel() { return fuel; } public void addFuel(float fuel) { this.fuel += fuel; } // Create a method above this line public Car(int modelYear, string brandName, string numberPlate, string ownerName, float fuel) : base("Car", modelYear) { this.brandName = brandName; this.numberPlate = numberPlate; this.ownerName = ownerName; this.fuel = fuel; } } class Program { static void Main() { // Create an instance of Car Car myCar = new Car(2022, "Toyota", "ABC123", "John Doe", 50.0f); // Accessing properties and methods from Car class Console.WriteLine($"Brand: {myCar.brandName}"); Console.WriteLine($"Number Plate: {myCar.numberPlate}"); // Accessing getModelYear method from Vehicle class Console.WriteLine($"Model Year: {myCar.getModelYear()}"); // Accessing getFuel method from Car class Console.WriteLine($"Fuel: {myCar.getFuel()}"); // Adding fuel using addFuel method myCar.addFuel(10.0f); Console.WriteLine($"After adding fuel, new Fuel: {myCar.getFuel()}"); } }
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 7

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Awesome!

Completion rate improved to 2.04

bookЗавдання: Інкапсуляція

Свайпніть щоб показати меню

Базовий код містить два класи: Vehicle та Car. Деякі поля потрібно приховати, а деякі — зробити доступними.

  • Відкоригувати видимість полів type та modelYear у класі Vehicle так, щоб вони були недоступні ззовні класу, включаючи похідні класи;
  • Створити новий метод під назвою getModelYear у класі Vehicle, який повертає значення modelYear. Цей метод має бути доступним звідусіль. Це дозволить користувачам класу отримувати значення modelYear, але не змінювати його ззовні;
  • Властивості ownerName та fuel мають бути недоступними з будь-якого місця;
  • Створити метод під назвою getFuel, який повертає значення fuel;
  • Створити метод під назвою addFuel, який приймає аргумент типу float з іменем fuel. Усередині методу додати значення fuel до властивості fuel (this.fuel).
index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
using System; class Vehicle { // Edit code below this line public string type; public int modelYear; // Edit code above this line // Create a method below this line // Create a method above this line public Vehicle(string type, int modelYear) { this.type = type; this.modelYear = modelYear; } } class Car : Vehicle { // Edit code below this line public string brandName; public string numberPlate; public string ownerName; public float fuel; // Edit code above this line // Create a method below this line // Create a method above this line public Car(int modelYear, string brandName, string numberPlate, string ownerName, float fuel) : base("Car", modelYear) { this.brandName = brandName; this.numberPlate = numberPlate; this.ownerName = ownerName; this.fuel = fuel; } } class Program { static void Main() { // Create an instance of Car Car myCar = new Car(2022, "Toyota", "ABC123", "John Doe", 50.0f); // Accessing properties and methods from Car class Console.WriteLine($"Brand: {myCar.brandName}"); Console.WriteLine($"Number Plate: {myCar.numberPlate}"); // Accessing getModelYear method from Vehicle class Console.WriteLine($"Model Year: {myCar.getModelYear()}"); // Accessing getFuel method from Car class Console.WriteLine($"Fuel: {myCar.getFuel()}"); // Adding fuel using addFuel method myCar.addFuel(10.0f); Console.WriteLine($"After adding fuel, new Fuel: {myCar.getFuel()}"); } }
  1. Методи getModelYear та getFuel є методами-геттерами. Вони не приймають аргументів, є public і лише повертають значення.
  2. Метод addFuel є методом-сеттером. Він є public і приймає один аргумент типу float з іменем fuel. Для оновлення значення пального, можливо, потрібно використати ключове слово this. (this.fuel += fuel)
index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
using System; class Vehicle { // Edit code below this line private string type; private int modelYear; // Edit code above this line // Create a method below this line public int getModelYear() { return modelYear; } // Create a method above this line public Vehicle(string type, int modelYear) { this.type = type; this.modelYear = modelYear; } } class Car : Vehicle { // Edit code below this line public string brandName; public string numberPlate; private string ownerName; private float fuel; // Edit code above this line // Create a method below this line public float getFuel() { return fuel; } public void addFuel(float fuel) { this.fuel += fuel; } // Create a method above this line public Car(int modelYear, string brandName, string numberPlate, string ownerName, float fuel) : base("Car", modelYear) { this.brandName = brandName; this.numberPlate = numberPlate; this.ownerName = ownerName; this.fuel = fuel; } } class Program { static void Main() { // Create an instance of Car Car myCar = new Car(2022, "Toyota", "ABC123", "John Doe", 50.0f); // Accessing properties and methods from Car class Console.WriteLine($"Brand: {myCar.brandName}"); Console.WriteLine($"Number Plate: {myCar.numberPlate}"); // Accessing getModelYear method from Vehicle class Console.WriteLine($"Model Year: {myCar.getModelYear()}"); // Accessing getFuel method from Car class Console.WriteLine($"Fuel: {myCar.getFuel()}"); // Adding fuel using addFuel method myCar.addFuel(10.0f); Console.WriteLine($"After adding fuel, new Fuel: {myCar.getFuel()}"); } }
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 7
some-alt