Desafí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.
- Definir una clase
ElectricVehicleque extiendaVehicle; - El constructor de
ElectricVehicledebe aceptarmake,model,yearybatteryCapacity; - Utilizar
super()para inicializarmake,modelyyearen la clase padre; - Agregar un método
getDetailsaElectricVehicleque sobrescriba el métodoVehicledegetDetails. Debe llamar asuper.getDetails()y añadir información sobrebatteryCapacity. El formato debe ser:"{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.
- Definir una clase
ElectricVehicleque extiendaVehicle; - En el constructor de
ElectricVehicle, agregar un parámetrobatteryCapacitydespués demake,modelyyear; - Utilizar
super(make, model, year)para llamar al constructor de la clase padre e inicializarmake,modelyyear; - Asignar
batteryCapacitya una propiedad enElectricVehicle; - Definir un método
getDetailsenElectricVehicleque sobrescriba el métodoVehicledegetDetails; - Dentro de
getDetails, utilizarsuper.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.
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.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
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
Desafí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.
- Definir una clase
ElectricVehicleque extiendaVehicle; - El constructor de
ElectricVehicledebe aceptarmake,model,yearybatteryCapacity; - Utilizar
super()para inicializarmake,modelyyearen la clase padre; - Agregar un método
getDetailsaElectricVehicleque sobrescriba el métodoVehicledegetDetails. Debe llamar asuper.getDetails()y añadir información sobrebatteryCapacity. El formato debe ser:"{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.
- Definir una clase
ElectricVehicleque extiendaVehicle; - En el constructor de
ElectricVehicle, agregar un parámetrobatteryCapacitydespués demake,modelyyear; - Utilizar
super(make, model, year)para llamar al constructor de la clase padre e inicializarmake,modelyyear; - Asignar
batteryCapacitya una propiedad enElectricVehicle; - Definir un método
getDetailsenElectricVehicleque sobrescriba el métodoVehicledegetDetails; - Dentro de
getDetails, utilizarsuper.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.
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.
¡Gracias por tus comentarios!