Desafio: Implemente Herança de Classes com Extends e Super()
Tarefa
Você está trabalhando em um sistema de gerenciamento de transporte que rastreia diferentes tipos de veículos. Cada veículo possui um make, model e year. Para veículos elétricos, também é necessário rastrear o batteryCapacity. Você utilizará herança para estender uma classe geral Vehicle para veículos elétricos específicos.
- Crie uma classe
ElectricVehicleque estendaVehicle; - O construtor de
ElectricVehicledeve aceitarmake,model,yearebatteryCapacity; - Use
super()para inicializarmake,modeleyearna classe pai; - Adicione um método
getDetailsemElectricVehicleque sobrescreva o métodoVehicledegetDetails. Ele deve chamarsuper.getDetails()e adicionar informações sobre obatteryCapacity. O formato deve 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 uma classe
ElectricVehicleque estendaVehicle; - No construtor de
ElectricVehicle, adicionar um parâmetrobatteryCapacityapósmake,modeleyear; - Utilizar
super(make, model, year)para chamar o construtor da classe pai e inicializarmake,modeleyear; - Atribuir
batteryCapacitya uma propriedade emElectricVehicle; - Definir um método
getDetailsemElectricVehicleque sobrescreva o métodoVehicledegetDetails; - Dentro de
getDetails, utilizarsuper.getDetails()para obter os detalhes da classe pai e, em seguida, adicionar a informação da capacidade da bateria à string 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.
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
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
Desafio: Implemente Herança de Classes com Extends e Super()
Deslize para mostrar o menu
Tarefa
Você está trabalhando em um sistema de gerenciamento de transporte que rastreia diferentes tipos de veículos. Cada veículo possui um make, model e year. Para veículos elétricos, também é necessário rastrear o batteryCapacity. Você utilizará herança para estender uma classe geral Vehicle para veículos elétricos específicos.
- Crie uma classe
ElectricVehicleque estendaVehicle; - O construtor de
ElectricVehicledeve aceitarmake,model,yearebatteryCapacity; - Use
super()para inicializarmake,modeleyearna classe pai; - Adicione um método
getDetailsemElectricVehicleque sobrescreva o métodoVehicledegetDetails. Ele deve chamarsuper.getDetails()e adicionar informações sobre obatteryCapacity. O formato deve 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 uma classe
ElectricVehicleque estendaVehicle; - No construtor de
ElectricVehicle, adicionar um parâmetrobatteryCapacityapósmake,modeleyear; - Utilizar
super(make, model, year)para chamar o construtor da classe pai e inicializarmake,modeleyear; - Atribuir
batteryCapacitya uma propriedade emElectricVehicle; - Definir um método
getDetailsemElectricVehicleque sobrescreva o métodoVehicledegetDetails; - Dentro de
getDetails, utilizarsuper.getDetails()para obter os detalhes da classe pai e, em seguida, adicionar a informação da capacidade da bateria à string 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.
Obrigado pelo seu feedback!