Appliquer la notion
Sur repl.it, exécuter le code TypeScript suivant :
1
class User {
2
private name: string;
3
private age: number;
4
5
public constructor(name: string, age: number) {
6
this.name = name;
7
this.age = age;
8
}
9
10
public getName() {
11
return this.name;
12
}
13
14
public getAge() {
15
return this.age;
16
}
17
}
18
19
let alice = new User('Alice', 25);
20
console.log(`${alice.getName()} a ${alice.getAge()} ans`);
Question
Faire une méthode birthday
qui affiche « Joyeux anniversaire [name] ! ».
Solution
1
class User {
2
private name: string;
3
private age: number;
4
5
public constructor(name: string, age: number) {
6
this.name = name;
7
this.age = age;
8
}
9
10
public getName() {
11
return this.name;
12
}
13
14
public getAge() {
15
return this.age;
16
}
17
18
public birthday() {
19
console.log(`Joyeux anniversaire ${this.name} !`);
20
}
21
}
22
let alice = new User("Alice", 25);
23
console.log(`${alice.getName()} a ${alice.getAge()} ans`);
24
alice.birthday();