Défi : Implémenter l’Héritage de Classe avec extends et super()
Tâche
Vous travaillez sur un système de gestion des transports qui suit différents types de véhicules. Chaque véhicule possède une make, un model et une year. Pour les véhicules électriques, il est également nécessaire de suivre la batteryCapacity. Vous utiliserez l'héritage pour étendre une classe générale Vehicle afin de gérer des véhicules électriques spécifiques.
- Créer une classe
ElectricVehiclequi étendVehicle; - Le constructeur de
ElectricVehicledoit acceptermake,model,yearetbatteryCapacity; - Utiliser
super()pour initialisermake,modeletyeardans la classe parente ; - Ajouter une méthode
getDetailsàElectricVehiclequi redéfinit la méthodeVehicledegetDetails. Elle doit appelersuper.getDetails()et ajouter l'information concernant labatteryCapacity. Le format doit être :"{make} {model} ({year}) with a battery capacity of {batteryCapacity} kWh.".
12345678910111213141516171819202122232425262728293031class Vehicle { constructor(make, model, year) { this.make = make; this.model = model; this.year = year; } getDetails() { return `${this.make} ${this.model} (${this.year})`; } } class ElectricVehicle _____ _____ { _____(make, model, year, _____) { _____(_____, _____, _____); this.batteryCapacity = batteryCapacity; } _____() { return `${super._____} with a battery capacity of ${ _____._____ } kWh.`; } } // Testing const vehicle = new Vehicle('Toyota', 'Camry', 2020); console.log(vehicle.getDetails()); // Expected: Toyota Camry (2020) const electricVehicle = new ElectricVehicle('Tesla', 'Model 3', 2021, 75); console.log(electricVehicle.getDetails()); // Expected: Tesla Model 3 (2021) with a battery capacity of 75 kWh.
- Définir une classe
ElectricVehiclequi étendVehicle; - Dans le constructeur de
ElectricVehicle, ajouter un paramètrebatteryCapacityaprèsmake,modeletyear; - Utiliser
super(make, model, year)pour appeler le constructeur de la classe parente et initialisermake,modeletyear; - Assigner
batteryCapacityà une propriété dansElectricVehicle; - Définir une méthode
getDetailsdansElectricVehiclequi redéfinit la méthodeVehicledegetDetails; - À l'intérieur de
getDetails, utilisersuper.getDetails()pour obtenir les détails de la classe parente, puis ajouter l'information de la capacité de la batterie à la chaîne retournée.
12345678910111213141516171819202122232425262728293031class Vehicle { constructor(make, model, year) { this.make = make; this.model = model; this.year = year; } getDetails() { return `${this.make} ${this.model} (${this.year})`; } } class ElectricVehicle extends Vehicle { constructor(make, model, year, batteryCapacity) { super(make, model, year); this.batteryCapacity = batteryCapacity; } getDetails() { return `${super.getDetails()} with a battery capacity of ${ this.batteryCapacity } kWh.`; } } // Testing const vehicle = new Vehicle('Toyota', 'Camry', 2020); console.log(vehicle.getDetails()); // Output: Toyota Camry (2020) const electricVehicle = new ElectricVehicle('Tesla', 'Model 3', 2021, 75); console.log(electricVehicle.getDetails()); // Output: Tesla Model 3 (2021) with a battery capacity of 75 kWh.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Can you explain how inheritance works in this example?
What does the super() function do in the constructor?
How does method overriding work in JavaScript classes?
Awesome!
Completion rate improved to 2.22
Défi : Implémenter l’Héritage de Classe avec extends et super()
Glissez pour afficher le menu
Tâche
Vous travaillez sur un système de gestion des transports qui suit différents types de véhicules. Chaque véhicule possède une make, un model et une year. Pour les véhicules électriques, il est également nécessaire de suivre la batteryCapacity. Vous utiliserez l'héritage pour étendre une classe générale Vehicle afin de gérer des véhicules électriques spécifiques.
- Créer une classe
ElectricVehiclequi étendVehicle; - Le constructeur de
ElectricVehicledoit acceptermake,model,yearetbatteryCapacity; - Utiliser
super()pour initialisermake,modeletyeardans la classe parente ; - Ajouter une méthode
getDetailsàElectricVehiclequi redéfinit la méthodeVehicledegetDetails. Elle doit appelersuper.getDetails()et ajouter l'information concernant labatteryCapacity. Le format doit être :"{make} {model} ({year}) with a battery capacity of {batteryCapacity} kWh.".
12345678910111213141516171819202122232425262728293031class Vehicle { constructor(make, model, year) { this.make = make; this.model = model; this.year = year; } getDetails() { return `${this.make} ${this.model} (${this.year})`; } } class ElectricVehicle _____ _____ { _____(make, model, year, _____) { _____(_____, _____, _____); this.batteryCapacity = batteryCapacity; } _____() { return `${super._____} with a battery capacity of ${ _____._____ } kWh.`; } } // Testing const vehicle = new Vehicle('Toyota', 'Camry', 2020); console.log(vehicle.getDetails()); // Expected: Toyota Camry (2020) const electricVehicle = new ElectricVehicle('Tesla', 'Model 3', 2021, 75); console.log(electricVehicle.getDetails()); // Expected: Tesla Model 3 (2021) with a battery capacity of 75 kWh.
- Définir une classe
ElectricVehiclequi étendVehicle; - Dans le constructeur de
ElectricVehicle, ajouter un paramètrebatteryCapacityaprèsmake,modeletyear; - Utiliser
super(make, model, year)pour appeler le constructeur de la classe parente et initialisermake,modeletyear; - Assigner
batteryCapacityà une propriété dansElectricVehicle; - Définir une méthode
getDetailsdansElectricVehiclequi redéfinit la méthodeVehicledegetDetails; - À l'intérieur de
getDetails, utilisersuper.getDetails()pour obtenir les détails de la classe parente, puis ajouter l'information de la capacité de la batterie à la chaîne retournée.
12345678910111213141516171819202122232425262728293031class Vehicle { constructor(make, model, year) { this.make = make; this.model = model; this.year = year; } getDetails() { return `${this.make} ${this.model} (${this.year})`; } } class ElectricVehicle extends Vehicle { constructor(make, model, year, batteryCapacity) { super(make, model, year); this.batteryCapacity = batteryCapacity; } getDetails() { return `${super.getDetails()} with a battery capacity of ${ this.batteryCapacity } kWh.`; } } // Testing const vehicle = new Vehicle('Toyota', 'Camry', 2020); console.log(vehicle.getDetails()); // Output: Toyota Camry (2020) const electricVehicle = new ElectricVehicle('Tesla', 'Model 3', 2021, 75); console.log(electricVehicle.getDetails()); // Output: Tesla Model 3 (2021) with a battery capacity of 75 kWh.
Merci pour vos commentaires !