forked from TrainingByPackt/Professional-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise48.js
More file actions
49 lines (43 loc) · 1.15 KB
/
Exercise48.js
File metadata and controls
49 lines (43 loc) · 1.15 KB
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
class Vehicle {
constructor(name, speed, cost) {
this.name = name;
this.speed = speed;
this.cost = cost;
}
start() {
console.log('Starting vehicle, ' + this.name + ' at ' + this.speed + 'km/h');
}
buy() {
console.log('Buying for ' + this.cost);
}
}
const vehicle = new Vehicle('bicycle', 15, 100);
vehicle.start();
vehicle.buy();
class Car extends Vehicle {
constructor(name, speed, cost, tankSize) {
super(name, speed, cost);
this.tankSize = tankSize;
}
start() {
console.log('Driving car, at ' + this.speed + 'km/h');
}
}
class Plane extends Vehicle {
start() {
console.log('Flying plane, at ' + this.speed + 'km/h');
}
}
class Rocket extends Vehicle {
start() {
console.log('Flying rocket to the moon, at ' + this.speed + 'km/h');
}
}
const car = new Car('Toyota Corolla', 120, 5000);
const plane = new Plane('Boeing 737', 1000, 26000000);
const rocket = new Rocket('Saturn V', 9920, 6000000000);
car.start();
plane.start();
rocket.start();
const car2 = new Car('Toyota Corolla 2', 120, 5000, 2000);
console.log(car2.tankSize); // 2000