-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathFun with ES6 Classes #2 - Animals and Inheritance
79 lines (70 loc) · 2.63 KB
/
Fun with ES6 Classes #2 - Animals and Inheritance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Description:
Fun with ES6 Classes #2 - Animals and Inheritance
Overview
Preloaded for you in this Kata is a class Animal:
class Animal {
constructor(name, age, legs, species, status) {
this.name = name;
this.age = age;
this.legs = legs;
this.species = species;
this.status = status;
}
introduce() {
return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
}
}
Task
Define the following classes that inherit from Animal.
I. Shark
The constructor function for Shark should accept 3 arguments in total in the following order: name, age, status. All sharks should have a leg count of 0 (since they obviously do not have any legs) and should have a species of "shark".
II. Cat
The constructor function for Cat should accept the same 3 arguments as with Shark: name, age, status. Cats should always have a leg count of 4 and a species of "cat".
Furthermore, the introduce/Introduce method for Cat should be identical to the original except there should be exactly 2 spaces and the words "Meow meow!" after the phrase. For example:
var example = new Cat("Example", 10, "Happy");
example.introduce() === "Hello, my name is Example and I am 10 years old. Meow meow!"; // Notice the TWO spaces - very important
III. Dog
The Dog constructor should accept 4 arguments in the specified order: name, age, status, master. master is the name of the dog's master which will be a string. Furthermore, dogs should have 4 legs and a species of "dog".
Dogs have an identical introduce/Introduce method as any other animal, but they have their own method called greetMaster/GreetMaster which accepts no arguments and returns "Hello (insert_master_name_here)" (of course not the literal string but replace the (insert_master_name_here) with the name of the dog's master).
*/
class Shark extends Animal {
constructor(name, age, status) {
super();
this.name = name;
this.age = age;
this.legs = 0;
this.status = status;
this.species = 'shark';
}
}
class Cat extends Animal {
constructor(name, age, status) {
super();
this.name = name;
this.age = age;
this.status = status;
this.legs = 4;
this.species = 'cat';
}
introduce() {
return `Hello, my name is ${this.name} and I am ${this.age} years old. Meow meow!`;
}
}
class Dog extends Animal {
constructor(name, age, status,master='') {
super();
this.master = master;
this.name = name;
this.age = age;
this.status = status;
this.legs = 4;
this.species = 'dog';
}
introduce() {
return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
}
greetMaster() {
return `Hello ${this.master}`;
}
}