Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Desafío: Implementar Herencia de Clases con Extends y Super() | Dominio de las Clases e Herencia en JavaScript
Maestría Avanzada en JavaScript

bookDesafío: Implementar Herencia de Clases con Extends y Super()

Desafío

Se está desarrollando un sistema de gestión de transporte que rastrea diferentes tipos de vehículos. Cada vehículo tiene un make, model y year. Para los vehículos eléctricos, también es necesario registrar la batteryCapacity. Se utilizará herencia para extender una clase general Vehicle para vehículos eléctricos específicos.

  1. Definir una clase ElectricVehicle que extienda Vehicle;
  2. El constructor de ElectricVehicle debe aceptar make, model, year y batteryCapacity;
  3. Utilizar super() para inicializar make, model y year en la clase padre;
  4. Agregar un método getDetails a ElectricVehicle que sobrescriba el método Vehicle de getDetails. Debe llamar a super.getDetails() y añadir información sobre batteryCapacity. El formato debe ser: "{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
  • Definir una clase ElectricVehicle que extienda Vehicle;
  • En el constructor de ElectricVehicle, agregar un parámetro batteryCapacity después de make, model y year;
  • Utilizar super(make, model, year) para llamar al constructor de la clase padre e inicializar make, model y year;
  • Asignar batteryCapacity a una propiedad en ElectricVehicle;
  • Definir un método getDetails en ElectricVehicle que sobrescriba el método Vehicle de getDetails;
  • Dentro de getDetails, utilizar super.getDetails() para obtener los detalles de la clase padre y luego añadir la información de la capacidad de la batería a la cadena de retorno.
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

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 15

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

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

bookDesafío: Implementar Herencia de Clases con Extends y Super()

Desliza para mostrar el menú

Desafío

Se está desarrollando un sistema de gestión de transporte que rastrea diferentes tipos de vehículos. Cada vehículo tiene un make, model y year. Para los vehículos eléctricos, también es necesario registrar la batteryCapacity. Se utilizará herencia para extender una clase general Vehicle para vehículos eléctricos específicos.

  1. Definir una clase ElectricVehicle que extienda Vehicle;
  2. El constructor de ElectricVehicle debe aceptar make, model, year y batteryCapacity;
  3. Utilizar super() para inicializar make, model y year en la clase padre;
  4. Agregar un método getDetails a ElectricVehicle que sobrescriba el método Vehicle de getDetails. Debe llamar a super.getDetails() y añadir información sobre batteryCapacity. El formato debe ser: "{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
  • Definir una clase ElectricVehicle que extienda Vehicle;
  • En el constructor de ElectricVehicle, agregar un parámetro batteryCapacity después de make, model y year;
  • Utilizar super(make, model, year) para llamar al constructor de la clase padre e inicializar make, model y year;
  • Asignar batteryCapacity a una propiedad en ElectricVehicle;
  • Definir un método getDetails en ElectricVehicle que sobrescriba el método Vehicle de getDetails;
  • Dentro de getDetails, utilizar super.getDetails() para obtener los detalles de la clase padre y luego añadir la información de la capacidad de la batería a la cadena de retorno.
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

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 15
some-alt