diff --git a/src/app.ts b/src/app.ts index 9bcceb1..d8a50ee 100644 --- a/src/app.ts +++ b/src/app.ts @@ -15,19 +15,43 @@ class Department { // this.id = '2'; // readonly이기 때문에 error가 발생한다. this.employees.push(employee); } + + printEmployeeInformation() { + console.log(this.employees.length); + console.log(this.employees); + } } class ITDepartment extends Department { admins: string[]; - constructor(id: string, public admin: string[]) { + constructor(id: string, admins: string[]) { super(id, 'IT'); - this.admins = admin; + this.admins = admins; } } class AccountingDepartment extends Department { + private lastReport: string; + + get mostRecentReport() { + if (this.lastReport) { + return this.lastReport; + } + throw new Error('No report found.'); + } + + set setMostRecentReport(value: string) { + if (!value) { + throw new Error('Please pass in a valid value!') + } + this.addReport(value); + this.lastReport = value; // 여기서 lastReport를 업데이트 그래야 lastReport가 비어있지 않기 때문에 정상적으로 동작을 한다. + } + constructor(id: string, private reports: string[]) { super(id, 'Account'); + //strictPropertyInitialization 활성화로 초기화 해줘야 함. + this.lastReport = reports[0] || ""; // 초기값을 할당 (reports가 비어있으면 빈 문자열) } addReport(text: string) { @@ -38,7 +62,6 @@ class AccountingDepartment extends Department { console.log(this.reports); } } - const accounting = new Department('1', 'Accounting'); const ITaccounting = new ITDepartment('2', ['Max']); @@ -51,6 +74,10 @@ ITaccounting.printEmployeeInformation(); const NewAccounting = new AccountingDepartment('d2', []); +// console.log(NewAccounting.mostRecentReport); //report가 추가되지 않아서 Error +NewAccounting.setMostRecentReport = 'Year End Report'; NewAccounting.addReport('Something went wrong...'); +console.log(NewAccounting.mostRecentReport); //report가 있어서 문제없이 출력 + NewAccounting.printReports();