Sfida: Implementare l'Ereditarietà delle Classi con Extends e Super()
Compito
Stai lavorando su un sistema di gestione dei trasporti che tiene traccia di diversi tipi di veicoli. Ogni veicolo ha un make, model e year. Per i veicoli elettrici, è necessario anche tracciare la batteryCapacity. Utilizzerai l'ereditarietà per estendere una classe generale Vehicle per veicoli elettrici specifici.
- Creare una classe
ElectricVehicleche estendeVehicle; - Il costruttore di
ElectricVehicledeve accettaremake,model,yearebatteryCapacity; - Utilizzare
super()per inizializzaremake,modeleyearnella classe genitore; - Aggiungere un metodo
getDetailsaElectricVehicleche sovrascrive il metodoVehicledigetDetails. Deve chiamaresuper.getDetails()e aggiungere informazioni sullabatteryCapacity. Il formato deve essere:"{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.
- Definire una classe
ElectricVehicleche estendeVehicle; - Nel costruttore di
ElectricVehicle, aggiungere un parametrobatteryCapacitydopomake,modeleyear; - Utilizzare
super(make, model, year)per chiamare il costruttore della classe genitore e inizializzaremake,modeleyear; - Assegnare
batteryCapacitya una proprietà inElectricVehicle; - Definire un metodo
getDetailsinElectricVehicleche sovrascrive il metodoVehicledigetDetails; - All'interno di
getDetails, utilizzaresuper.getDetails()per ottenere i dettagli della classe genitore, quindi aggiungere le informazioni sulla capacità della batteria alla stringa restituita.
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.
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
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
Sfida: Implementare l'Ereditarietà delle Classi con Extends e Super()
Scorri per mostrare il menu
Compito
Stai lavorando su un sistema di gestione dei trasporti che tiene traccia di diversi tipi di veicoli. Ogni veicolo ha un make, model e year. Per i veicoli elettrici, è necessario anche tracciare la batteryCapacity. Utilizzerai l'ereditarietà per estendere una classe generale Vehicle per veicoli elettrici specifici.
- Creare una classe
ElectricVehicleche estendeVehicle; - Il costruttore di
ElectricVehicledeve accettaremake,model,yearebatteryCapacity; - Utilizzare
super()per inizializzaremake,modeleyearnella classe genitore; - Aggiungere un metodo
getDetailsaElectricVehicleche sovrascrive il metodoVehicledigetDetails. Deve chiamaresuper.getDetails()e aggiungere informazioni sullabatteryCapacity. Il formato deve essere:"{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.
- Definire una classe
ElectricVehicleche estendeVehicle; - Nel costruttore di
ElectricVehicle, aggiungere un parametrobatteryCapacitydopomake,modeleyear; - Utilizzare
super(make, model, year)per chiamare il costruttore della classe genitore e inizializzaremake,modeleyear; - Assegnare
batteryCapacitya una proprietà inElectricVehicle; - Definire un metodo
getDetailsinElectricVehicleche sovrascrive il metodoVehicledigetDetails; - All'interno di
getDetails, utilizzaresuper.getDetails()per ottenere i dettagli della classe genitore, quindi aggiungere le informazioni sulla capacità della batteria alla stringa restituita.
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.
Grazie per i tuoi commenti!