-
Notifications
You must be signed in to change notification settings - Fork 0
/
19-Class_Getters_Setters.ts
70 lines (58 loc) · 1.57 KB
/
19-Class_Getters_Setters.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
64
65
66
67
68
69
70
class ClassGettersSettersDepartment {
protected employees: string[] = [];
constructor(private readonly id: string, public name: string) {}
describe(this: ClassGettersSettersDepartment) {
console.log('Department:' + `${this.id}: ${this.name}`);
}
addEmployee(employee: string) {
// validation etc
if (!employee) return;
this.employees.push(employee);
}
printReport() {
console.log(this.employees);
}
}
class ExtendClassGettersSettersDepartment extends ClassGettersSettersDepartment {
private lastReport: string;
get mostRecentReport() {
if (this.lastReport) {
return this.lastReport;
}
throw new Error('No report found');
}
set mostRecentReport(value: string) {
if (!value) {
throw new Error('Please input some value!');
}
this.addReport(value);
}
constructor(id: string, private reports: string[]) {
super(id, 'Accounting');
this.lastReport = reports[0];
}
addEmployee(name: string) {
if (name === 'Max') {
return;
}
this.employees.push(name);
}
addReport(text: string) {
this.reports.push(text);
this.lastReport = text;
}
printReport() {
console.log(this.reports);
}
}
export function run19() {
let employee = new ExtendClassGettersSettersDepartment('id01', []);
// employee.lastReport = '' // Error
employee.addReport('Bonjour!');
employee.addReport('hello');
// employee.addReport('world');
console.log(employee.mostRecentReport);
employee.mostRecentReport = 'World!';
console.log(employee.mostRecentReport);
employee.printReport();
}