Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Défi : Encapsulation | Principes de la POO
C# Au-Delà des Bases

bookDéfi : Encapsulation

Le code de base comporte deux classes appelées Vehicle et Car. Certains champs doivent être cachés tandis que d'autres doivent être exposés.

  • Ajuster la visibilité des champs type et modelYear dans Vehicle afin qu'ils ne soient pas accessibles depuis l'extérieur de la classe, y compris les classes dérivées ;
  • Créer une nouvelle méthode appelée getModelYear à l'intérieur de la classe Vehicle afin qu'elle retourne la valeur de modelYear. Cette méthode doit être accessible depuis n'importe où. Cette méthode permettra aux utilisateurs de cette classe d'accéder à modelYear sans pouvoir le modifier depuis l'extérieur ;
  • Les propriétés ownerName et fuel ne doivent être accessibles depuis aucun endroit ;
  • Créer une méthode appelée getFuel qui retourne la valeur de fuel ;
  • Créer une méthode appelée addFuel qui prend en argument un float nommé fuel. À l'intérieur de la méthode, ajouter la valeur de fuel à la propriété 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. Les méthodes getModelYear et getFuel sont des accesseurs (getters). Elles ne prennent aucun argument, sont public et retournent uniquement une valeur.
  2. La méthode addFuel est un mutateur (setter). Elle est public et prend un argument float nommé fuel. Pour mettre à jour la valeur de fuel, il peut être nécessaire d'utiliser le mot-clé 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()}"); } }
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 5. Chapitre 7

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

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

bookDéfi : Encapsulation

Glissez pour afficher le menu

Le code de base comporte deux classes appelées Vehicle et Car. Certains champs doivent être cachés tandis que d'autres doivent être exposés.

  • Ajuster la visibilité des champs type et modelYear dans Vehicle afin qu'ils ne soient pas accessibles depuis l'extérieur de la classe, y compris les classes dérivées ;
  • Créer une nouvelle méthode appelée getModelYear à l'intérieur de la classe Vehicle afin qu'elle retourne la valeur de modelYear. Cette méthode doit être accessible depuis n'importe où. Cette méthode permettra aux utilisateurs de cette classe d'accéder à modelYear sans pouvoir le modifier depuis l'extérieur ;
  • Les propriétés ownerName et fuel ne doivent être accessibles depuis aucun endroit ;
  • Créer une méthode appelée getFuel qui retourne la valeur de fuel ;
  • Créer une méthode appelée addFuel qui prend en argument un float nommé fuel. À l'intérieur de la méthode, ajouter la valeur de fuel à la propriété 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. Les méthodes getModelYear et getFuel sont des accesseurs (getters). Elles ne prennent aucun argument, sont public et retournent uniquement une valeur.
  2. La méthode addFuel est un mutateur (setter). Elle est public et prend un argument float nommé fuel. Pour mettre à jour la valeur de fuel, il peut être nécessaire d'utiliser le mot-clé 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()}"); } }
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 5. Chapitre 7
some-alt