-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.ts
63 lines (52 loc) · 1.17 KB
/
a.ts
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
class Car {
name: string;
acceleration: number = 0;
constructor(name: string) {
this.name = name;
}
honk() {
console.log("Toooooooot!");
}
accelerate(speed: number) {
this.acceleration = this.acceleration + speed;
}
}
const car = new Car("BMW");
car.honk();
console.log(car.acceleration);
car.accelerate(20);
console.log(car.acceleration);
// Exercise 2 - Inheritance
class BaseObject {
width = 0;
length = 0;
}
class Rectangle extends BaseObject {
calcSize() {
return this.width * this.length;
}
}
const rectangle = new Rectangle();
rectangle.width = 5;
rectangle.length = 10;
console.log(rectangle.calcSize());
// Exercise 3 - Getters & Setters
class Person {
private _firstName: string = "";
get firstName() {
return this._firstName;
}
set firstName(value: string) {
if (value.length > 3) {
this._firstName = value;
} else {
this._firstName = "";
}
}
}
const person = new Person();
console.log(person.firstName);
person.firstName = "Ma";
console.log(person.firstName);
person.firstName = "Maximilian";
console.log(person.firstName);