Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Sfida: Parola Chiave `static` | Fondamenti Di OOP
C# Oltre le Basi

bookSfida: Parola Chiave `static`

In questo esercizio è necessario:

  • Creare un nuovo campo privato chiamato totalCars di tipo double che tenga traccia del numero totale di oggetti Car creati;
  • Creare un metodo getter chiamato getTotalCars che restituisce semplicemente il numero di oggetti;
  • Assicurarsi che il valore del campo totalCars venga incrementato ogni volta che viene creato un nuovo oggetto Car.
index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738
using System; class Car { int modelYear; double mileage; string brandName; public Car(string brandName, int modelYear, double mileage) { this.brandName = brandName; this.modelYear = modelYear; this.mileage = mileage; // Write code below this line // Write code above this line } // Write code below this line // Write code above this line } class ConsoleApp { static void Main() { Console.WriteLine(Car.getTotalCars()); Car car1 = new Car("Toyota", 2022, 25.5); Car car2 = new Car("Honda", 2020, 30.2); Car car3 = new Car("Ford", 2021, 28.8); Console.WriteLine(Car.getTotalCars()); } }
  1. Il campo che tiene traccia del numero di oggetti deve essere statico perché memorizza sempre alcuni dati.
  2. Nel capitolo sui Modificatori di Accesso è stato spiegato che un metodo getter è semplicemente un metodo pubblico che restituisce il valore di un campo privato. In questo caso getTotalCars dovrebbe restituire totalCars.
  3. Anche il metodo getTotalCars deve essere statico perché si desidera utilizzarlo senza un'istanza.
index.cs

index.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142
using System; class Car { int modelYear; double mileage; string brandName; public Car(string brandName, int modelYear, double mileage) { this.brandName = brandName; this.modelYear = modelYear; this.mileage = mileage; // Write code below this line totalCars += 1; // Write code above this line } // Write code below this line private static double totalCars; public static double getTotalCars() { return totalCars; } // Write code above this line } class ConsoleApp { static void Main() { Console.WriteLine(Car.getTotalCars()); Car car1 = new Car("Toyota", 2022, 25.5); Car car2 = new Car("Honda", 2020, 30.2); Car car3 = new Car("Ford", 2021, 28.8); Console.WriteLine(Car.getTotalCars()); } }
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 6

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 how to declare a static private field in Java?

How do I increment the static field in the constructor?

Can you explain why the getter method should be static?

Awesome!

Completion rate improved to 2.04

bookSfida: Parola Chiave `static`

Scorri per mostrare il menu

In questo esercizio è necessario:

  • Creare un nuovo campo privato chiamato totalCars di tipo double che tenga traccia del numero totale di oggetti Car creati;
  • Creare un metodo getter chiamato getTotalCars che restituisce semplicemente il numero di oggetti;
  • Assicurarsi che il valore del campo totalCars venga incrementato ogni volta che viene creato un nuovo oggetto Car.
index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738
using System; class Car { int modelYear; double mileage; string brandName; public Car(string brandName, int modelYear, double mileage) { this.brandName = brandName; this.modelYear = modelYear; this.mileage = mileage; // Write code below this line // Write code above this line } // Write code below this line // Write code above this line } class ConsoleApp { static void Main() { Console.WriteLine(Car.getTotalCars()); Car car1 = new Car("Toyota", 2022, 25.5); Car car2 = new Car("Honda", 2020, 30.2); Car car3 = new Car("Ford", 2021, 28.8); Console.WriteLine(Car.getTotalCars()); } }
  1. Il campo che tiene traccia del numero di oggetti deve essere statico perché memorizza sempre alcuni dati.
  2. Nel capitolo sui Modificatori di Accesso è stato spiegato che un metodo getter è semplicemente un metodo pubblico che restituisce il valore di un campo privato. In questo caso getTotalCars dovrebbe restituire totalCars.
  3. Anche il metodo getTotalCars deve essere statico perché si desidera utilizzarlo senza un'istanza.
index.cs

index.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142
using System; class Car { int modelYear; double mileage; string brandName; public Car(string brandName, int modelYear, double mileage) { this.brandName = brandName; this.modelYear = modelYear; this.mileage = mileage; // Write code below this line totalCars += 1; // Write code above this line } // Write code below this line private static double totalCars; public static double getTotalCars() { return totalCars; } // Write code above this line } class ConsoleApp { static void Main() { Console.WriteLine(Car.getTotalCars()); Car car1 = new Car("Toyota", 2022, 25.5); Car car2 = new Car("Honda", 2020, 30.2); Car car3 = new Car("Ford", 2021, 28.8); Console.WriteLine(Car.getTotalCars()); } }
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 6
some-alt