Skip to content

Instantly share code, notes, and snippets.

@bhubr
Created October 12, 2020 14:17
Show Gist options
  • Save bhubr/c2597117b2a08a32f3c10d62c50b3aa1 to your computer and use it in GitHub Desktop.
Save bhubr/c2597117b2a08a32f3c10d62c50b3aa1 to your computer and use it in GitHub Desktop.
Exemple programmation orientée objet
class Animal {
constructor(species, name) {
this.species = species;
this.name = name;
}
displayFeatures() {
console.log(`I am ${this.name} and I'm a ${this.species}`);
}
}
class Mammal extends Animal {
run() {
console.log('I am running');
}
}
class Feline extends Mammal {
run() {
console.log('I am hunting peaceful cute animals');
}
}
class Rodent extends Mammal {
run() {
console.log('I am running away from predators!');
}
}
class Fish extends Animal {
swim() {
console.log('I am swimming');
}
}
class Bird extends Animal {
fly() {
console.log('I am flying');
}
}
const grosMinet = new Feline('cat', 'Gros-Minet');
grosMinet.displayFeatures();
grosMinet.run();
const tom = new Feline('cat', 'Tom');
tom.displayFeatures();
tom.run();
const jerry = new Rodent('mouse', 'Jerry');
jerry.displayFeatures();
jerry.run();
const nemo = new Fish('goldfish', 'Nemo');
nemo.displayFeatures();
nemo.swim();
const titi = new Bird('yellowbird', 'Titi');
titi.displayFeatures();
titi.fly();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment