Haaste: Kapselointi
Pohjakoodissa on kaksi luokkaa nimeltä Vehicle
ja Car
. Osa kentistä täytyy piilottaa, kun taas osa täytyy altistaa näkyville.
- Säädä
type
-luokanmodelYear
- jaVehicle
-kenttien näkyvyyttä siten, että niihin ei pääse käsiksi luokan ulkopuolelta, mukaan lukien perityt luokat; - Luo uusi metodi nimeltä
getModelYear
Vehicle
-luokkaan siten, että se palauttaamodelYear
-kentän arvon. Tämän metodin tulee olla käytettävissä kaikkialta. Tämän metodin avulla luokan käyttäjät voivat hakeamodelYear
-arvon, mutta eivät voi muuttaa sitä ulkopuolelta; ownerName
- jafuel
-ominaisuudet eivät saa olla käytettävissä mistään;- Luo metodi nimeltä
getFuel
, joka palauttaafuel
-arvon; - Luo metodi nimeltä
addFuel
, joka ottaa parametrinafloat
-tyyppisen argumentin nimeltäfuel
. Metodin sisällä lisää argumentinfuel
arvon ominaisuuteenfuel
(this.fuel
).
index.cs
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364using 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()}"); } }
- Metodit
getModelYear
jagetFuel
ovat getter-menetelmiä. Ne eivät ota parametreja, ovatpublic
ja palauttavat vain arvon. - Metodi
addFuel
on setter-menetelmä. Se onpublic
ja ottaa yhdenfloat
-parametrin nimeltäfuel
. Polttoaineen arvon päivittämiseen saatat tarvitathis
-avainsanaa. (this.fuel += fuel
)
index.cs
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576using 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()}"); } }
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 5. Luku 7
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
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
Haaste: Kapselointi
Pyyhkäise näyttääksesi valikon
Pohjakoodissa on kaksi luokkaa nimeltä Vehicle
ja Car
. Osa kentistä täytyy piilottaa, kun taas osa täytyy altistaa näkyville.
- Säädä
type
-luokanmodelYear
- jaVehicle
-kenttien näkyvyyttä siten, että niihin ei pääse käsiksi luokan ulkopuolelta, mukaan lukien perityt luokat; - Luo uusi metodi nimeltä
getModelYear
Vehicle
-luokkaan siten, että se palauttaamodelYear
-kentän arvon. Tämän metodin tulee olla käytettävissä kaikkialta. Tämän metodin avulla luokan käyttäjät voivat hakeamodelYear
-arvon, mutta eivät voi muuttaa sitä ulkopuolelta; ownerName
- jafuel
-ominaisuudet eivät saa olla käytettävissä mistään;- Luo metodi nimeltä
getFuel
, joka palauttaafuel
-arvon; - Luo metodi nimeltä
addFuel
, joka ottaa parametrinafloat
-tyyppisen argumentin nimeltäfuel
. Metodin sisällä lisää argumentinfuel
arvon ominaisuuteenfuel
(this.fuel
).
index.cs
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364using 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()}"); } }
- Metodit
getModelYear
jagetFuel
ovat getter-menetelmiä. Ne eivät ota parametreja, ovatpublic
ja palauttavat vain arvon. - Metodi
addFuel
on setter-menetelmä. Se onpublic
ja ottaa yhdenfloat
-parametrin nimeltäfuel
. Polttoaineen arvon päivittämiseen saatat tarvitathis
-avainsanaa. (this.fuel += fuel
)
index.cs
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576using 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()}"); } }
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 5. Luku 7