“Komposisi Polimorfisme Polimorfisme Enkapsulasi JavaScript” Kode Jawaban

Komposisi Polimorfisme Polimorfisme Enkapsulasi 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

Komposisi Polimorfisme Polimorfisme Enkapsulasi 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

Komposisi Polimorfisme Polimorfisme Enkapsulasi 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

Komposisi Polimorfisme Polimorfisme Enkapsulasi 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

Komposisi Polimorfisme Polimorfisme Enkapsulasi 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

Komposisi Polimorfisme Polimorfisme Enkapsulasi 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

Komposisi Polimorfisme Polimorfisme Enkapsulasi 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

Komposisi Polimorfisme Polimorfisme Enkapsulasi 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

Komposisi Polimorfisme Polimorfisme Enkapsulasi 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

Komposisi Polimorfisme Polimorfisme Enkapsulasi 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

Jawaban yang mirip dengan “Komposisi Polimorfisme Polimorfisme Enkapsulasi JavaScript”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya