How to use Methods of a Extends Class?

Asked

Viewed 76 times

0

I have two classes : A and B

Class B is like this: B extends A

I need to use some methods that has within the Class To, in class B

.

How I do?

class NeorisMapBi {
    constructor(mapID, mapOBJ){
        this._mapID = mapID;
        this._mapOBJ = mapOBJ;
    }

    get MapBi(){
        return this._mapOBJ;
    };
}

class CustomControl extends NeorisMapBi {
    constructor(divID, divCLASS, divPOSITION, buttonID, buttonCLASS, buttonTITLE, buttonINNERHTML){
        this._divID = divID;
        this._divCLASS = divCLASS;
        this._divPOSITION = divPOSITION;

        this._buttonID = buttonID;
        this._buttonCLASS = buttonCLASS;
        this._buttonTITLE = buttonTITLE;
        this._buttonINNERHTML = buttonINNERHTML;  
    } 
}
  • Have you tried super.MapBi(); ?

1 answer

2

According to the documentation, super is used to access the parent object of an object.

Define getters and setters.

...
set velocidade(km) {
    this.km = km;
}
get velocidade() {
    console.log(`Veículo ${this.modelo} esta se movimentando a ${this.km} KM/h!`);
}
...

To use, just call normally.

// Set
gol.velocidade = 20;
// Get
gol.velocidade;

Take this example:

class Carro {
  constructor(modelo) {
    this.modelo = modelo;
    this.km = 0;
  }
  set velocidade(km) {
    this.km = km;
  }
  get velocidade() {
    console.log(`Veículo ${this.modelo} esta se movimentando a ${this.km} KM/h!`);
  }
  get movimentar() {
    console.log(`Veículo ${this.modelo} em movimento!`);
  }
  
  parar() {
    console.log(`Veículo ${this.modelo} parou!`);
  }
}

class Palio extends Carro {
  abastecer() {
    super.parar();
    console.log(`Veículo ${this.modelo} está abastecendo!`);
    super.movimentar;
  }
}

class Mobi extends Palio {
  // recebe como parâmetro: modelo, step
  constructor(modelo, step) {
    // Aqui, ele chama a classe construtora pai com o modelo
    super(modelo);
    this.step = step;
    console.log(`Veículo ${this.modelo} ${this.step}!`);
  }
  abastecer() {
    console.log(`Veículo ${this.modelo} está abastecendo!`);
  }
}

let gol = new Carro('Gol');
gol.movimentar;
gol.velocidade = 20;
gol.velocidade;
gol.parar();
gol.velocidade = 0;
gol.velocidade;

let palio = new Palio('Palio Fire');
palio.abastecer();

let mobi = new Mobi('Mobi Like', 'Não tem step');
mobi.movimentar;
mobi.parar();
mobi.abastecer();
mobi.movimentar;

  • When I make use of the super in no method get up, it returns me undefiened.

  • @Thiagodeboniscarvalhosaad, see now. I exemplified the use.

  • so in the class that was extended I can’t have a builder is this?

  • You can, I’ll edit.

  • ah I think I got it.. So the Construct in this extended class the "model" is the attribute of the parent class and the "step" is the attribute of the daughter class?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.