“Composição de polimorfismo de encapsulamento JavaScript” Respostas de código

Composição de polimorfismo de encapsulamento JavaScript

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Ndumiso Zwane

Composição de polimorfismo de encapsulamento JavaScript

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Ndumiso Zwane

Composição de polimorfismo de encapsulamento JavaScript

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Ndumiso Zwane

Composição de polimorfismo de encapsulamento JavaScript

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Ndumiso Zwane

Composição de polimorfismo de encapsulamento JavaScript

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Ndumiso Zwane

Composição de polimorfismo de encapsulamento JavaScript

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Ndumiso Zwane

Composição de polimorfismo de encapsulamento JavaScript

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Ndumiso Zwane

Composição de polimorfismo de encapsulamento JavaScript

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Ndumiso Zwane

Composição de polimorfismo de encapsulamento JavaScript

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Ndumiso Zwane

Composição de polimorfismo de encapsulamento JavaScript

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Ndumiso Zwane

Respostas semelhantes a “Composição de polimorfismo de encapsulamento JavaScript”

Procure respostas de código populares por idioma

Procurar outros idiomas de código