// AccountingDepartment サブクラス
class AccountingDepartment extends Department {
private lastReport: string;
//Getterで、privateフィールドを外部から取得できるようにする
get mostRecentReport() {
if (this.lastReport) {
return this.lastReport;
}
throw new Error('レポートが見つかりませんm(__)m');
}
//Setterで、privateフィールドを外部から変更できるようにする
set mostRecentReport(value: string) {
if (!value) {
throw new Error('正しい値を設定してください!');
}
this.addReport(value); //最新のレポートを追加
}
constructor(id: string, private reports: string[]) {
super(id, 'Accounting');
this.lastReport = reports[0];
}
addReport(text: string) {
this.reports.push(text);
this.lastReport = text;
}
}
const accounting = new AccountingDepartment('d2', []);
console.log(accounting.mostRecentReport); //レポートが見つかりません。
//レポートを追加
accounting.addReport('新しいレポートだよ');
//Getterで値を取得
console.log(accounting.mostRecentReport); //新しいレポートだよ
accounting.mostRecentReport = ''; //正しい値を設定してください!
//Setterで値を設定
accounting.mostRecentReport = '重要なレポート';
accounting.addReport('とても重要なレポート');
console.log(accounting.mostRecentReport); //とても重要なレポート