Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Desafio: Implemente Herança de Classes com Extends e Super() | Dominando Classes e Herança em JavaScript
Domínio Avançado de JavaScript

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

  1. Crie uma classe ElectricVehicle que estenda Vehicle;
  2. O construtor de ElectricVehicle deve aceitar make, model, year e batteryCapacity;
  3. Use super() para inicializar make, model e year na classe pai;
  4. Adicione um método getDetails em ElectricVehicle que sobrescreva o método Vehicle de getDetails. Ele deve chamar super.getDetails() e adicionar informações sobre o batteryCapacity. O formato deve 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 uma classe ElectricVehicle que estenda Vehicle;
  • No construtor de ElectricVehicle, adicionar um parâmetro batteryCapacity após make, model e year;
  • Utilizar super(make, model, year) para chamar o construtor da classe pai e inicializar make, model e year;
  • Atribuir batteryCapacity a uma propriedade em ElectricVehicle;
  • Definir um método getDetails em ElectricVehicle que sobrescreva o método Vehicle de getDetails;
  • Dentro de getDetails, utilizar super.getDetails() para obter os detalhes da classe pai e, em seguida, adicionar a informação da capacidade da bateria à string 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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 15

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 2.22

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

  1. Crie uma classe ElectricVehicle que estenda Vehicle;
  2. O construtor de ElectricVehicle deve aceitar make, model, year e batteryCapacity;
  3. Use super() para inicializar make, model e year na classe pai;
  4. Adicione um método getDetails em ElectricVehicle que sobrescreva o método Vehicle de getDetails. Ele deve chamar super.getDetails() e adicionar informações sobre o batteryCapacity. O formato deve 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 uma classe ElectricVehicle que estenda Vehicle;
  • No construtor de ElectricVehicle, adicionar um parâmetro batteryCapacity após make, model e year;
  • Utilizar super(make, model, year) para chamar o construtor da classe pai e inicializar make, model e year;
  • Atribuir batteryCapacity a uma propriedade em ElectricVehicle;
  • Definir um método getDetails em ElectricVehicle que sobrescreva o método Vehicle de getDetails;
  • Dentro de getDetails, utilizar super.getDetails() para obter os detalhes da classe pai e, em seguida, adicionar a informação da capacidade da bateria à string 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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 15
some-alt