forked from milq/milq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06.js
245 lines (169 loc) · 4.02 KB
/
06.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
'use strict'
class Point
{
constructor(x, y)
{
this._x = x
this._y = y
}
get x()
{
return this._x
}
set x(x)
{
this._x = x
}
get y()
{
return this._y
}
set y(y)
{
this._y = y
}
}
class Character
{
constructor(name, max_health)
{
this._name = name
this._x = 50
this._y = 50
this._max_health = max_health
this._health = max_health
this._speed = 10
this._attack = 8
this._defend = 4
}
get name()
{
return this._name
}
get x()
{
return this._x
}
get health()
{
return this._health
}
get attack()
{
return this._attack
}
move_right()
{
this._x = this._x + this._speed
}
move_left()
{
this._x = this._x - this._speed
}
take_damage(damage)
{
this._health = this._health - damage
}
}
class Animal
{
constructor(name, energy)
{
this._name = name
this._energy = energy
}
get name()
{
return this._name
}
get energy()
{
return this._energy
}
eat()
{
this._energy = this._energy + 5
}
move()
{
this._energy = this._energy - 5
}
}
class Dog extends Animal
{
constructor(name, energy, sound, breed)
{
super(name, energy)
this._sound = sound
this._breed = breed
}
get sound()
{
return this._sound
}
get breed()
{
return this._breed
}
}
class Cat extends Animal
{
constructor(name, energy)
{
super(name, energy)
this._sound = 'Miaow!'
}
get sound()
{
return this._sound
}
purr()
{
this._sound = 'Purr...'
}
}
// EXAMPLE WITH CLASS POINT
console.log(' ')
console.log('POINTS')
console.log('------')
var point = new Point(2, 3) // We create an instance (or object) of class Point.
console.log(point.x, point.y)
point.x = 5
point.y = -4
console.log(point.x, point.y)
// EXAMPLE WITH CLASS CHARACTER
console.log(' ')
console.log('WELCOME, PLAYER!')
console.log('----------------')
var hero = new Character('Reinwald', 50) // We create an instance (or object) of class Character.
var evil = new Character('Zarosh', 40) // We create an instance (or object) of class Character.
hero.move_right()
hero.move_right()
console.log('The current x position of', hero.name, 'is', hero.x)
evil.take_damage(hero.attack)
console.log('The current hit points of', evil.name, 'is', evil.health)
// EXAMPLE WITH CLASS ANIMAL AND CLASSES DOG AND CAT (inheritance)
console.log(' ')
console.log('ANIMALS')
console.log('-------')
var ruperta = new Cat('Ruperta', 100) // We create an instance (or object) of class Cat.
var chloe = new Cat('Chloe', 75) // We create an instance (or object) of class Cat.
var ruperta_energy = ruperta.energy
console.log('The energy of', ruperta.name, 'is', ruperta_energy, 'and her sound is', ruperta.sound)
console.log('The energy of', chloe.name, 'is', chloe.energy, 'and her sound is', chloe.sound)
chloe.move()
chloe.eat()
chloe.move()
ruperta.purr()
console.log('Now, the energy of', chloe.name, 'is', chloe.energy)
console.log('Now, the sound of', ruperta.name, 'is', ruperta.sound)
var odessa = new Animal('Odessa', 50) // We create an instance (or object) of class Animal.
odessa.move()
odessa.move()
odessa.move()
console.log('Suddenly,', odessa.name, 'with', odessa.energy, 'of energy appeared!')
var toby = new Dog('Toby', 50, 'Woof!', 'Golden Retriever') // We create an instance (or object) of class Dog.
var akira = new Dog('Akira', 125, 'Woof, woof!', 'Siberian Husky') // We create an instance (or object) of class Dog.
console.log(toby.name, 'is a', toby.breed, 'and his sound is', toby.sound)
console.log(akira.name, 'is a', akira.breed, 'and his sound is', akira.sound)
console.log(' ')