Advertisement
superiqbal7

HummingBird OOP

Feb 1st, 2023
951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 0.97 KB | Source Code | 0 0
  1. interface Flyable {
  2.   fly: ()=> void
  3. }
  4.  
  5.  
  6. class Animal {
  7.   constructor(public name: string) {}
  8. }
  9.  
  10. class Mamal extends Animal {}
  11.  
  12. class Bird extends Animal implements Flyable {
  13.   constructor(name: string) {
  14.     super(name);
  15.   }
  16.  
  17.   fly(): void {
  18.     console.log(`${this.name} the bird is flying`);
  19.   }
  20. }
  21.  
  22. class Bat extends Mamal implements Flyable {
  23.   constructor(name: string) {
  24.     super(name);
  25.   }
  26.  
  27.   fly(): void {
  28.     console.log(`${this.name} is flying like bats`);
  29.   }
  30. }
  31.  
  32. class HummingBirdFactory {
  33.   static createHummingBird(bird: Bird, bat: Bat): HummingBird {
  34.     return new HummingBird(bird.name, bat.fly);
  35.   }
  36. }
  37.  
  38. class HummingBird {
  39.   name: string;
  40.   fly: () => void;
  41.  
  42.   constructor(name: string, fly: () => void) {
  43.     this.name = name;
  44.     this.fly = fly;
  45.   }
  46. }
  47.  
  48. const bird = new Bird('Humming Bird');
  49. const bat = new Bat('Bat');
  50. const hummingBird = HummingBirdFactory.createHummingBird(bird, bat);
  51.  
  52. hummingBird.fly();
  53.  
  54.  
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement