Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Sfida: Incapsulamento | Principi OOP
C# Oltre le Basi

bookSfida: Incapsulamento

Il codice di base contiene due classi chiamate Vehicle e Car. Alcuni dei campi devono essere nascosti mentre altri devono essere esposti.

  • Modificare la visibilità dei campi type e modelYear in Vehicle in modo che non siano accessibili dall'esterno della classe, incluse le classi derivate;
  • Creare un nuovo metodo chiamato getModelYear all'interno della classe Vehicle che restituisca il valore di modelYear. Questo metodo deve essere accessibile da qualsiasi luogo. Questo metodo permetterà agli utenti di questa classe di accedere a modelYear ma non di modificarlo dall'esterno;
  • Le proprietà ownerName e fuel non devono essere accessibili da nessuna parte;
  • Creare un metodo chiamato getFuel che restituisca il valore di fuel;
  • Creare un metodo chiamato addFuel che accetti un argomento di tipo float chiamato fuel. All'interno del metodo, aggiungere il valore di fuel alla proprietà 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. I metodi getModelYear e getFuel sono metodi getter. Non accettano argomenti, sono public e restituiscono solo un valore.
  2. Il metodo addFuel è un metodo setter. È public e accetta un argomento di tipo float chiamato fuel. Per aggiornare il valore di fuel potrebbe essere necessario utilizzare la parola chiave 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()}"); } }
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 5. Capitolo 7

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you show me the base code for the `Vehicle` and `Car` classes?

Could you clarify which programming language this is for?

Do you need an explanation of how to implement field visibility in classes?

Awesome!

Completion rate improved to 2.04

bookSfida: Incapsulamento

Scorri per mostrare il menu

Il codice di base contiene due classi chiamate Vehicle e Car. Alcuni dei campi devono essere nascosti mentre altri devono essere esposti.

  • Modificare la visibilità dei campi type e modelYear in Vehicle in modo che non siano accessibili dall'esterno della classe, incluse le classi derivate;
  • Creare un nuovo metodo chiamato getModelYear all'interno della classe Vehicle che restituisca il valore di modelYear. Questo metodo deve essere accessibile da qualsiasi luogo. Questo metodo permetterà agli utenti di questa classe di accedere a modelYear ma non di modificarlo dall'esterno;
  • Le proprietà ownerName e fuel non devono essere accessibili da nessuna parte;
  • Creare un metodo chiamato getFuel che restituisca il valore di fuel;
  • Creare un metodo chiamato addFuel che accetti un argomento di tipo float chiamato fuel. All'interno del metodo, aggiungere il valore di fuel alla proprietà 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. I metodi getModelYear e getFuel sono metodi getter. Non accettano argomenti, sono public e restituiscono solo un valore.
  2. Il metodo addFuel è un metodo setter. È public e accetta un argomento di tipo float chiamato fuel. Per aggiornare il valore di fuel potrebbe essere necessario utilizzare la parola chiave 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()}"); } }
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 5. Capitolo 7
some-alt