Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Sfida: Implementare l'Ereditarietà delle Classi con Extends e Super() | Padronanza delle Classi e dell'Ereditarietà in JavaScript
Padronanza Avanzata di JavaScript

bookSfida: 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.

  1. Creare una classe ElectricVehicle che estende Vehicle;
  2. Il costruttore di ElectricVehicle deve accettare make, model, year e batteryCapacity;
  3. Utilizzare super() per inizializzare make, model e year nella classe genitore;
  4. Aggiungere un metodo getDetails a ElectricVehicle che sovrascrive il metodo Vehicle di getDetails. Deve chiamare super.getDetails() e aggiungere informazioni sulla batteryCapacity. Il formato deve essere: "{make} {model} ({year}) with a battery capacity of {batteryCapacity} kWh.".
12345678910111213141516171819202122232425262728293031
class 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.
copy
  • Definire una classe ElectricVehicle che estende Vehicle;
  • Nel costruttore di ElectricVehicle, aggiungere un parametro batteryCapacity dopo make, model e year;
  • Utilizzare super(make, model, year) per chiamare il costruttore della classe genitore e inizializzare make, model e year;
  • Assegnare batteryCapacity a una proprietà in ElectricVehicle;
  • Definire un metodo getDetails in ElectricVehicle che sovrascrive il metodo Vehicle di getDetails;
  • All'interno di getDetails, utilizzare super.getDetails() per ottenere i dettagli della classe genitore, quindi aggiungere le informazioni sulla capacità della batteria alla stringa restituita.
12345678910111213141516171819202122232425262728293031
class 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.
copy

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 15

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 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

bookSfida: 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.

  1. Creare una classe ElectricVehicle che estende Vehicle;
  2. Il costruttore di ElectricVehicle deve accettare make, model, year e batteryCapacity;
  3. Utilizzare super() per inizializzare make, model e year nella classe genitore;
  4. Aggiungere un metodo getDetails a ElectricVehicle che sovrascrive il metodo Vehicle di getDetails. Deve chiamare super.getDetails() e aggiungere informazioni sulla batteryCapacity. Il formato deve essere: "{make} {model} ({year}) with a battery capacity of {batteryCapacity} kWh.".
12345678910111213141516171819202122232425262728293031
class 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.
copy
  • Definire una classe ElectricVehicle che estende Vehicle;
  • Nel costruttore di ElectricVehicle, aggiungere un parametro batteryCapacity dopo make, model e year;
  • Utilizzare super(make, model, year) per chiamare il costruttore della classe genitore e inizializzare make, model e year;
  • Assegnare batteryCapacity a una proprietà in ElectricVehicle;
  • Definire un metodo getDetails in ElectricVehicle che sovrascrive il metodo Vehicle di getDetails;
  • All'interno di getDetails, utilizzare super.getDetails() per ottenere i dettagli della classe genitore, quindi aggiungere le informazioni sulla capacità della batteria alla stringa restituita.
12345678910111213141516171819202122232425262728293031
class 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.
copy

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 15
some-alt