From e6f6938ecc8770103031c76d5bb3720a97a6a7e0 Mon Sep 17 00:00:00 2001 From: toshi-ue Date: Wed, 12 Jun 2024 14:03:01 +0900 Subject: [PATCH 01/15] =?UTF-8?q?59.=E6=9C=80=E5=88=9D=E3=81=AE=E3=82=AF?= =?UTF-8?q?=E3=83=A9=E3=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/app.ts b/src/app.ts index e69de29..14d1851 100644 --- a/src/app.ts +++ b/src/app.ts @@ -0,0 +1,14 @@ +class Department { + name: string; + // 初期値も設定できる + // name: string = 'Default' + + // constructorメソッド + // 予約語。 + constructor(n: string) { + this.name = n; + } +} + +const accounting = new Department('Accounting'); +console.log(accounting) From 6ac640ab8d2b8ca5472a912167306f24a9910529 Mon Sep 17 00:00:00 2001 From: toshi-ue Date: Wed, 12 Jun 2024 14:53:28 +0900 Subject: [PATCH 02/15] =?UTF-8?q?61.=E3=82=B3=E3=83=B3=E3=82=B9=E3=83=88?= =?UTF-8?q?=E3=83=A9=E3=82=AF=E3=82=BF=E9=96=A2=E6=95=B0=E3=81=A8=20`this`?= =?UTF-8?q?=E3=82=AD=E3=83=BC=E3=83=AF=E3=83=BC=E3=83=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/app.ts b/src/app.ts index 14d1851..deb3458 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,3 +1,39 @@ +// class Department { +// name: string; +// // 初期値も設定できる +// // name: string = 'Default' + +// // constructorメソッド +// // 予約語。 +// constructor(n: string) { +// this.name = n; +// } + +// describe() { +// // 以下の書き方だとクラスの外のグローバル変数を参照するため正しく動作しない +// // console.log('Department' + name); +// // クラスのプロパティを参照するにはthisを使用する +// // ここでのthisはクラスから作成された具体的なプロパティを参照する +// console.log('Department: ' + this.name); + +// } +// } + +// const accounting = new Department('Accounting'); + +// accounting.describe() + +// // describeメソッドをオブジェクトに対して追加しているため期待した動作にならない +// // オプジェクトリテラルで作られたオブジェクトでありクラスに基づいて作成されたオブジェクトではない +// // クラスを使わずに単なるダミーオブジェクトとして作られたもの +// // describeプロパティの値はaccountingオブジェクトのdescribeメソッドを指している +// // 実行はしていないので関数の実行結果の値をdescribeに渡しているのではない +// // Department: undefined と表示される +// const accountingCopy = { describe: accounting.describe } +// // ここではaccountigCopyのdescribeメソッドを参照しているが、thisプロパティが存在しないため undefined になる +// accountingCopy.describe() + + class Department { name: string; // 初期値も設定できる @@ -8,7 +44,21 @@ class Department { constructor(n: string) { this.name = n; } + + describe(this: Department) { + console.log('Department: ' + this.name); + } } const accounting = new Department('Accounting'); -console.log(accounting) + +accounting.describe() + +// describeにthisを追加することでエラーを検知してくれる +// const accountingCopy = { describe: accounting.describe } +// accountingCopy.describe() + +// 以下は正しく動作する +// Departmentと同じようにnameプロパティを持っていて全く同じ構造のため +const accountingCopy = { name: 'dummy', describe: accounting.describe } +accountingCopy.describe() From 0e4597c1c476f6a44a0a9a9816882aa7dc690415 Mon Sep 17 00:00:00 2001 From: toshi-ue Date: Wed, 12 Jun 2024 16:50:19 +0900 Subject: [PATCH 03/15] =?UTF-8?q?62.privat=E4=BF=AE=E9=A3=BE=E5=AD=90?= =?UTF-8?q?=E3=81=A8public=E4=BF=AE=E9=A3=BE=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 67 ++++++++++++++++-------------------------------------- 1 file changed, 20 insertions(+), 47 deletions(-) diff --git a/src/app.ts b/src/app.ts index deb3458..a0dfdf8 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,46 +1,9 @@ -// class Department { -// name: string; -// // 初期値も設定できる -// // name: string = 'Default' - -// // constructorメソッド -// // 予約語。 -// constructor(n: string) { -// this.name = n; -// } - -// describe() { -// // 以下の書き方だとクラスの外のグローバル変数を参照するため正しく動作しない -// // console.log('Department' + name); -// // クラスのプロパティを参照するにはthisを使用する -// // ここでのthisはクラスから作成された具体的なプロパティを参照する -// console.log('Department: ' + this.name); - -// } -// } - -// const accounting = new Department('Accounting'); - -// accounting.describe() - -// // describeメソッドをオブジェクトに対して追加しているため期待した動作にならない -// // オプジェクトリテラルで作られたオブジェクトでありクラスに基づいて作成されたオブジェクトではない -// // クラスを使わずに単なるダミーオブジェクトとして作られたもの -// // describeプロパティの値はaccountingオブジェクトのdescribeメソッドを指している -// // 実行はしていないので関数の実行結果の値をdescribeに渡しているのではない -// // Department: undefined と表示される -// const accountingCopy = { describe: accounting.describe } -// // ここではaccountigCopyのdescribeメソッドを参照しているが、thisプロパティが存在しないため undefined になる -// accountingCopy.describe() - - +// private & public 修飾子 class Department { name: string; - // 初期値も設定できる - // name: string = 'Default' + // privateをつけることで外部からアクセスできないようにできる + private employees: string[] = []; - // constructorメソッド - // 予約語。 constructor(n: string) { this.name = n; } @@ -48,17 +11,27 @@ class Department { describe(this: Department) { console.log('Department: ' + this.name); } + + addEmployee(employee: string) { + this.employees.push(employee); + } + + printEmployeeInformation() { + console.log(this.employees.length); + console.log(this.employees); + } } const accounting = new Department('Accounting'); +accounting.addEmployee('Max') +accounting.addEmployee('Manu') + +// 外部(クラスの外部も含む)から直接変更できてしまう +// accounting.employees[2] = 'Anna' + accounting.describe() +accounting.printEmployeeInformation(); -// describeにthisを追加することでエラーを検知してくれる -// const accountingCopy = { describe: accounting.describe } +// const accountingCopy = { name: 'dummy', describe: accounting.describe } // accountingCopy.describe() - -// 以下は正しく動作する -// Departmentと同じようにnameプロパティを持っていて全く同じ構造のため -const accountingCopy = { name: 'dummy', describe: accounting.describe } -accountingCopy.describe() From ef69d04123d4da3954a0fbcb1de77a1a95406889 Mon Sep 17 00:00:00 2001 From: toshi-ue Date: Wed, 12 Jun 2024 17:03:01 +0900 Subject: [PATCH 04/15] =?UTF-8?q?63.=E3=83=97=E3=83=AD=E3=83=91=E3=83=86?= =?UTF-8?q?=E3=82=A3=E5=88=9D=E6=9C=9F=E5=8C=96=E3=81=AE=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=BC=E3=83=88=E3=82=AB=E3=83=83=E3=83=88=E6=A7=8B=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/app.ts b/src/app.ts index a0dfdf8..9256dca 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,15 +1,20 @@ -// private & public 修飾子 +// 63.プロパティ初期化のショートカット構文 class Department { - name: string; + // constructorを使用することで省略できる + // private id: string + // name: string; // privateをつけることで外部からアクセスできないようにできる private employees: string[] = []; - constructor(n: string) { - this.name = n; + // constructorに初期値を設定するだけで良い + constructor(private id: string, public name: string) { + // 以下も記述する必要がなくなる + // this.id = id; + // this.name = name; } describe(this: Department) { - console.log('Department: ' + this.name); + console.log(`Department (${this.id}): ${this.name}`); } addEmployee(employee: string) { @@ -22,14 +27,11 @@ class Department { } } -const accounting = new Department('Accounting'); +const accounting = new Department('d1', 'New Game'); accounting.addEmployee('Max') accounting.addEmployee('Manu') -// 外部(クラスの外部も含む)から直接変更できてしまう -// accounting.employees[2] = 'Anna' - accounting.describe() accounting.printEmployeeInformation(); From a3b169819ada87e4a05331833dd7ffbce2cbc328 Mon Sep 17 00:00:00 2001 From: toshi-ue Date: Thu, 13 Jun 2024 11:11:21 +0900 Subject: [PATCH 05/15] =?UTF-8?q?64.=20readonly=E3=83=97=E3=83=AD=E3=83=91?= =?UTF-8?q?=E3=83=86=E3=82=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 23 +---------------------- src/app.ts | 4 ++-- 2 files changed, 3 insertions(+), 24 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index b75fabb..0967ef4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,22 +1 @@ -{ - "workbench.colorCustomizations": { - "activityBar.activeBackground": "#2f7c47", - "activityBar.background": "#2f7c47", - "activityBar.foreground": "#e7e7e7", - "activityBar.inactiveForeground": "#e7e7e799", - "activityBarBadge.background": "#422c74", - "activityBarBadge.foreground": "#e7e7e7", - "commandCenter.border": "#e7e7e799", - "sash.hoverBorder": "#2f7c47", - "statusBar.background": "#215732", - "statusBar.foreground": "#e7e7e7", - "statusBarItem.hoverBackground": "#2f7c47", - "statusBarItem.remoteBackground": "#215732", - "statusBarItem.remoteForeground": "#e7e7e7", - "titleBar.activeBackground": "#215732", - "titleBar.activeForeground": "#e7e7e7", - "titleBar.inactiveBackground": "#21573299", - "titleBar.inactiveForeground": "#e7e7e799" - }, - "peacock.color": "#215732" -} +{} diff --git a/src/app.ts b/src/app.ts index 9256dca..f55a27d 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,4 +1,4 @@ -// 63.プロパティ初期化のショートカット構文 +// 64. readonlyプロパティ class Department { // constructorを使用することで省略できる // private id: string @@ -7,7 +7,7 @@ class Department { private employees: string[] = []; // constructorに初期値を設定するだけで良い - constructor(private id: string, public name: string) { + constructor(private readonly id: string, public name: string) { // 以下も記述する必要がなくなる // this.id = id; // this.name = name; From dd1dc3f28f84c3677ef8e10a483843c138e01873 Mon Sep 17 00:00:00 2001 From: toshi-ue Date: Thu, 13 Jun 2024 11:52:59 +0900 Subject: [PATCH 06/15] =?UTF-8?q?65.=E7=B6=99=E6=89=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/app.ts b/src/app.ts index f55a27d..3563a2f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,17 +1,8 @@ -// 64. readonlyプロパティ +// 65.継承 class Department { - // constructorを使用することで省略できる - // private id: string - // name: string; - // privateをつけることで外部からアクセスできないようにできる private employees: string[] = []; - // constructorに初期値を設定するだけで良い - constructor(private readonly id: string, public name: string) { - // 以下も記述する必要がなくなる - // this.id = id; - // this.name = name; - } + constructor(private readonly id: string, public name: string) { } describe(this: Department) { console.log(`Department (${this.id}): ${this.name}`); @@ -27,13 +18,19 @@ class Department { } } -const accounting = new Department('d1', 'New Game'); +class ITDepartment extends Department { + constructor(id: string, private admins: string[]) { + super(id, 'IT') + this.admins = admins; + } +} + +const it = new ITDepartment('d1', ['Max']); -accounting.addEmployee('Max') -accounting.addEmployee('Manu') +it.addEmployee('Max') +it.addEmployee('Manu') -accounting.describe() -accounting.printEmployeeInformation(); +it.describe() +it.printEmployeeInformation(); -// const accountingCopy = { name: 'dummy', describe: accounting.describe } -// accountingCopy.describe() +console.log(it); From 3115f5fc7e78e1560a6c72664aa51dd8aa05ce65 Mon Sep 17 00:00:00 2001 From: toshi-ue Date: Thu, 13 Jun 2024 13:20:26 +0900 Subject: [PATCH 07/15] =?UTF-8?q?66.=E3=83=97=E3=83=AD=E3=83=91=E3=83=86?= =?UTF-8?q?=E3=82=A3=E3=83=BC=E3=81=AE=E3=82=AA=E3=83=BC=E3=83=90=E3=83=BC?= =?UTF-8?q?=E3=83=A9=E3=82=A4=E3=83=89=20&=20protected=E4=BF=AE=E9=A3=BE?= =?UTF-8?q?=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/app.ts b/src/app.ts index 3563a2f..121457b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,6 +1,6 @@ -// 65.継承 +// 66.プロパティーのオーバーライド & protected修飾子 class Department { - private employees: string[] = []; + protected employees: string[] = []; constructor(private readonly id: string, public name: string) { } @@ -25,8 +25,30 @@ class ITDepartment extends Department { } } +class AccountingDepartment extends Department { + constructor(id: string, private reports: string[]) { + super(id, 'Accounting') + } + + addReport(text: string) { + this.reports.push(text) + } + + printReports() { + console.log(this.reports); + } + + addEmployee(name: string) { + if (name === 'Max') { + return + } + this.employees.push(name); + } +} + const it = new ITDepartment('d1', ['Max']); + it.addEmployee('Max') it.addEmployee('Manu') @@ -34,3 +56,13 @@ it.describe() it.printEmployeeInformation(); console.log(it); + +const accounting = new AccountingDepartment('d2', []); +accounting.addReport('Something') +accounting.printReports() + +accounting.addEmployee('Max') +accounting.addEmployee('Manu') + +accounting.describe() +accounting.printEmployeeInformation(); From 38263f389c32d918f5ec6412804f59fdda7f2692 Mon Sep 17 00:00:00 2001 From: toshi-ue Date: Thu, 13 Jun 2024 13:44:05 +0900 Subject: [PATCH 08/15] 67. Getter & Setter --- src/app.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/app.ts b/src/app.ts index 121457b..2220a83 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,4 +1,5 @@ -// 66.プロパティーのオーバーライド & protected修飾子 +// 67. Getter & Setter +// getter, setterを使えばprivateなプロパティでも外部から読み書きができるようになる。 class Department { protected employees: string[] = []; @@ -26,12 +27,31 @@ class ITDepartment extends Department { } class AccountingDepartment extends Department { + private lastReport: string; + + get mostRecentReport() { + if (this.lastReport) { + return this.lastReport + } + + throw new Error("レポートが見つかりません。"); + } + + 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; } printReports() { @@ -58,7 +78,10 @@ it.printEmployeeInformation(); console.log(it); const accounting = new AccountingDepartment('d2', []); + +accounting.mostRecentReport = 'abc' accounting.addReport('Something') +console.log(accounting.mostRecentReport); accounting.printReports() accounting.addEmployee('Max') From 5640561d42c6dbf991dba298ec20a62e8d00f310 Mon Sep 17 00:00:00 2001 From: toshi-ue Date: Thu, 13 Jun 2024 15:07:09 +0900 Subject: [PATCH 09/15] =?UTF-8?q?68.=20static=20=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89=20&=20=E3=83=97=E3=83=AD=E3=83=91=E3=83=86?= =?UTF-8?q?=E3=82=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/app.ts b/src/app.ts index 2220a83..4800f03 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,9 +1,22 @@ -// 67. Getter & Setter -// getter, setterを使えばprivateなプロパティでも外部から読み書きができるようになる。 +// 68. static メソッド & プロパティ class Department { + // 静的プロパティを追加 + static fiscalYear = 2020; + protected employees: string[] = []; - constructor(private readonly id: string, public name: string) { } + // 静的メソッドを追加 + static createEmployee(name: string) { + return { name: name }; + } + + // constructorはstaticにすることができない + constructor(private readonly id: string, public name: string) { + // staticでないところ(static修飾子が付与されていないところ)からはアクセスできない + // console.log(this.fiscalYear); + // もしアクセスしたいなら以下のように書く + // console.log(Department.fiscalYear); + } describe(this: Department) { console.log(`Department (${this.id}): ${this.name}`); @@ -66,6 +79,10 @@ class AccountingDepartment extends Department { } } +const employee1 = Department.createEmployee('Max') +console.log(employee1, Department.fiscalYear); + + const it = new ITDepartment('d1', ['Max']); From 77fa013000ebd0b0e42d7bbf69c917b82f5f72df Mon Sep 17 00:00:00 2001 From: toshi-ue Date: Thu, 13 Jun 2024 15:43:42 +0900 Subject: [PATCH 10/15] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AA=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=82=92=E5=89=8A=E9=99=A4=EF=BC=88=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/app.ts b/src/app.ts index 4800f03..63eaf14 100644 --- a/src/app.ts +++ b/src/app.ts @@ -5,17 +5,13 @@ class Department { protected employees: string[] = []; - // 静的メソッドを追加 static createEmployee(name: string) { return { name: name }; } - // constructorはstaticにすることができない constructor(private readonly id: string, public name: string) { - // staticでないところ(static修飾子が付与されていないところ)からはアクセスできない - // console.log(this.fiscalYear); - // もしアクセスしたいなら以下のように書く - // console.log(Department.fiscalYear); + console.log(Department.fiscalYear); + } describe(this: Department) { @@ -33,7 +29,8 @@ class Department { } class ITDepartment extends Department { - constructor(id: string, private admins: string[]) { + admins: string[]; + constructor(id: string, admins: string[]) { super(id, 'IT') this.admins = admins; } @@ -62,6 +59,10 @@ class AccountingDepartment extends Department { this.lastReport = reports[0]; } + describe() { + console.log('会計部門 - ID: ' + this.id); + } + addReport(text: string) { this.reports.push(text) this.lastReport = text; @@ -105,4 +106,4 @@ accounting.addEmployee('Max') accounting.addEmployee('Manu') accounting.describe() -accounting.printEmployeeInformation(); +// accounting.printEmployeeInformation(); From 113e06e3d5ee0dd490190da7b64f7a94aad59c17 Mon Sep 17 00:00:00 2001 From: toshi-ue Date: Thu, 13 Jun 2024 15:47:48 +0900 Subject: [PATCH 11/15] =?UTF-8?q?69.=20abstract=20=E3=82=AF=E3=83=A9?= =?UTF-8?q?=E3=82=B9=EF=BC=88=E6=8A=BD=E8=B1=A1=E3=82=AF=E3=83=A9=E3=82=B9?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/app.ts b/src/app.ts index 63eaf14..150af0b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,6 +1,5 @@ -// 68. static メソッド & プロパティ -class Department { - // 静的プロパティを追加 +// 69. abstract クラス(抽象クラス) +abstract class Department { static fiscalYear = 2020; protected employees: string[] = []; @@ -9,14 +8,23 @@ class Department { return { name: name }; } - constructor(private readonly id: string, public name: string) { + constructor(protected readonly id: string, public name: string) { console.log(Department.fiscalYear); - } - describe(this: Department) { - console.log(`Department (${this.id}): ${this.name}`); - } + // クラス名にもabstractをつけないと以下のようなエラーが表示される + // Abstract methods can only appear within an abstract class.ts(1244) + // 解決するにはclassにabstractをつける + // classにabstractをつけるとdescribeに以下のようなエラーが表示される + // Method 'describe' cannot have an implementation because it is marked abstract.ts(1245) + // abstractクラスはメソッドを追加できないということなので以下を行う + // 中かっこを削除 + // 戻り値を指定する + // 以下のコードはabstractを付与して以下を強制している + // - メソッド名がdescribeであること + // - thisのオブジェクトはDepartmentクラスかそのサブクラスであること + // - 戻り値がvoidであること + abstract describe(this: Department): void; addEmployee(employee: string) { this.employees.push(employee); @@ -28,12 +36,21 @@ class Department { } } +// 上でabstractが使われると以下のエラーが発生する +// Non-abstract class 'ITDepartment' does not implement all abstract members of 'Department'ts(18052) +// Non-abstract class 'ITDepartment' does not implement inherited abstract member 'describe' from class 'Department'. +// abstractクラスで定義されているdescribeメソッドを追加してあげればエラーがなくなる class ITDepartment extends Department { admins: string[]; constructor(id: string, admins: string[]) { super(id, 'IT') this.admins = admins; } + + describe() { + console.log('IT部門 - ID: ' + this.id); + + } } class AccountingDepartment extends Department { From 150a2c47c2c948d753efd3f2919477ce15c77796 Mon Sep 17 00:00:00 2001 From: toshi-ue Date: Thu, 13 Jun 2024 15:48:18 +0900 Subject: [PATCH 12/15] =?UTF-8?q?=E3=83=90=E3=83=83=E3=82=AF=E3=82=A2?= =?UTF-8?q?=E3=83=83=E3=83=97=E7=94=A8=E3=81=AE=E3=83=95=E3=82=A1=E3=82=A4?= =?UTF-8?q?=E3=83=AB=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/currentBackup.ts | 126 ++++++++++++++++++++++++++++++++++++++++++ src/previousBackup.ts | 108 ++++++++++++++++++++++++++++++++++++ 2 files changed, 234 insertions(+) create mode 100644 src/currentBackup.ts create mode 100644 src/previousBackup.ts diff --git a/src/currentBackup.ts b/src/currentBackup.ts new file mode 100644 index 0000000..c67a321 --- /dev/null +++ b/src/currentBackup.ts @@ -0,0 +1,126 @@ +// // 68. static メソッド & プロパティ +// abstract class Department { +// static fiscalYear = 2020; + +// protected employees: string[] = []; + +// static createEmployee(name: string) { +// return { name: name }; +// } + +// constructor(protected readonly id: string, public name: string) { +// console.log(Department.fiscalYear); +// } + +// // クラス名にもabstractをつけないと以下のようなエラーが表示される +// // Abstract methods can only appear within an abstract class.ts(1244) +// // 解決するにはclassにabstractをつける +// // classにabstractをつけるとdescribeに以下のようなエラーが表示される +// // Method 'describe' cannot have an implementation because it is marked abstract.ts(1245) +// // abstractクラスはメソッドを追加できないということなので以下を行う +// // 中かっこを削除 +// // 戻り値を指定する +// // 以下のコードはabstractを付与して以下を強制している +// // - メソッド名がdescribeであること +// // - thisのオブジェクトはDepartmentクラスかそのサブクラスであること +// // - 戻り値がvoidであること +// abstract describe(this: Department): void; + +// addEmployee(employee: string) { +// this.employees.push(employee); +// } + +// printEmployeeInformation() { +// console.log(this.employees.length); +// console.log(this.employees); +// } +// } + +// // 上でabstractが使われると以下のエラーが発生する +// // Non-abstract class 'ITDepartment' does not implement all abstract members of 'Department'ts(18052) +// // Non-abstract class 'ITDepartment' does not implement inherited abstract member 'describe' from class 'Department'. +// // abstractクラスで定義されているdescribeメソッドを追加してあげればエラーがなくなる +// class ITDepartment extends Department { +// admins: string[]; +// constructor(id: string, admins: string[]) { +// super(id, 'IT') +// this.admins = admins; +// } + +// describe() { +// console.log('IT部門 - ID: ' + this.id); + +// } +// } + +// class AccountingDepartment extends Department { +// private lastReport: string; + +// get mostRecentReport() { +// if (this.lastReport) { +// return this.lastReport +// } + +// throw new Error("レポートが見つかりません。"); +// } + +// 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]; +// } + +// describe() { +// console.log('会計部門 - ID: ' + this.id); +// } + +// addReport(text: string) { +// this.reports.push(text) +// this.lastReport = text; +// } + +// printReports() { +// console.log(this.reports); +// } + +// addEmployee(name: string) { +// if (name === 'Max') { +// return +// } +// this.employees.push(name); +// } +// } + +// const employee1 = Department.createEmployee('Max') +// console.log(employee1, Department.fiscalYear); + + +// const it = new ITDepartment('d1', ['Max']); + + +// it.addEmployee('Max') +// it.addEmployee('Manu') + +// it.describe() +// it.printEmployeeInformation(); + +// console.log(it); + +// const accounting = new AccountingDepartment('d2', []); + +// accounting.mostRecentReport = 'abc' +// accounting.addReport('Something') +// console.log(accounting.mostRecentReport); +// accounting.printReports() + +// accounting.addEmployee('Max') +// accounting.addEmployee('Manu') + +// accounting.describe() +// accounting.printEmployeeInformation(); diff --git a/src/previousBackup.ts b/src/previousBackup.ts new file mode 100644 index 0000000..0ad8781 --- /dev/null +++ b/src/previousBackup.ts @@ -0,0 +1,108 @@ +// // 68. static メソッド & プロパティ +// class Department { +// // 静的プロパティを追加 +// static fiscalYear = 2020; + +// protected employees: string[] = []; + +// // 静的メソッドを追加 +// static createEmployee(name: string) { +// return { name: name }; +// } + +// // constructorはstaticにすることができない +// constructor(private readonly id: string, public name: string) { +// // staticでないところ(static修飾子が付与されていないところ)からはアクセスできない +// // console.log(this.fiscalYear); +// // もしアクセスしたいなら以下のように書く +// // console.log(Department.fiscalYear); +// } + +// describe(this: Department) { +// console.log(`Department (${this.id}): ${this.name}`); +// } + +// addEmployee(employee: string) { +// this.employees.push(employee); +// } + +// printEmployeeInformation() { +// console.log(this.employees.length); +// console.log(this.employees); +// } +// } + +// class ITDepartment extends Department { +// constructor(id: string, private admins: string[]) { +// super(id, 'IT') +// this.admins = admins; +// } +// } + +// class AccountingDepartment extends Department { +// private lastReport: string; + +// get mostRecentReport() { +// if (this.lastReport) { +// return this.lastReport +// } + +// throw new Error("レポートが見つかりません。"); +// } + +// 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; +// } + +// printReports() { +// console.log(this.reports); +// } + +// addEmployee(name: string) { +// if (name === 'Max') { +// return +// } +// this.employees.push(name); +// } +// } + +// const employee1 = Department.createEmployee('Max') +// console.log(employee1, Department.fiscalYear); + + +// const it = new ITDepartment('d1', ['Max']); + + +// it.addEmployee('Max') +// it.addEmployee('Manu') + +// it.describe() +// it.printEmployeeInformation(); + +// console.log(it); + +// const accounting = new AccountingDepartment('d2', []); + +// accounting.mostRecentReport = 'abc' +// accounting.addReport('Something') +// console.log(accounting.mostRecentReport); +// accounting.printReports() + +// accounting.addEmployee('Max') +// accounting.addEmployee('Manu') + +// accounting.describe() +// accounting.printEmployeeInformation(); From 7e2f93436725b23c3bc2cfe6578cef1a7820ac9d Mon Sep 17 00:00:00 2001 From: toshi-ue Date: Thu, 13 Jun 2024 15:51:03 +0900 Subject: [PATCH 13/15] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AA=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/src/app.ts b/src/app.ts index 150af0b..e9bc378 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,4 +1,4 @@ -// 69. abstract クラス(抽象クラス) +// 70. シングルトン & private コンストラクタ abstract class Department { static fiscalYear = 2020; @@ -12,18 +12,6 @@ abstract class Department { console.log(Department.fiscalYear); } - // クラス名にもabstractをつけないと以下のようなエラーが表示される - // Abstract methods can only appear within an abstract class.ts(1244) - // 解決するにはclassにabstractをつける - // classにabstractをつけるとdescribeに以下のようなエラーが表示される - // Method 'describe' cannot have an implementation because it is marked abstract.ts(1245) - // abstractクラスはメソッドを追加できないということなので以下を行う - // 中かっこを削除 - // 戻り値を指定する - // 以下のコードはabstractを付与して以下を強制している - // - メソッド名がdescribeであること - // - thisのオブジェクトはDepartmentクラスかそのサブクラスであること - // - 戻り値がvoidであること abstract describe(this: Department): void; addEmployee(employee: string) { @@ -36,10 +24,6 @@ abstract class Department { } } -// 上でabstractが使われると以下のエラーが発生する -// Non-abstract class 'ITDepartment' does not implement all abstract members of 'Department'ts(18052) -// Non-abstract class 'ITDepartment' does not implement inherited abstract member 'describe' from class 'Department'. -// abstractクラスで定義されているdescribeメソッドを追加してあげればエラーがなくなる class ITDepartment extends Department { admins: string[]; constructor(id: string, admins: string[]) { From 9cf27c6f051753f08b9a16bb785a3851ad3e62e6 Mon Sep 17 00:00:00 2001 From: toshi-ue Date: Thu, 13 Jun 2024 16:38:16 +0900 Subject: [PATCH 14/15] =?UTF-8?q?70.=20=E3=82=B7=E3=83=B3=E3=82=B0?= =?UTF-8?q?=E3=83=AB=E3=83=88=E3=83=B3=20&=20private=20=E3=82=B3=E3=83=B3?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=83=A9=E3=82=AF=E3=82=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/app.ts b/src/app.ts index e9bc378..a58edf1 100644 --- a/src/app.ts +++ b/src/app.ts @@ -40,6 +40,14 @@ class ITDepartment extends Department { class AccountingDepartment extends Department { private lastReport: string; + // インスタンスを保持しておくためのprivateなstaticフィールドを追加する + // 以下はインスタンスというプロパティ名 + // このクラス自体が持っているフィールをを表す + // privateなフィールドなのでこのクラスの中からのみアクセスできる + // フィールドの方は、このクラスのオブジェクト + // getInstanceメソッドで使用する + private static instance: AccountingDepartment; + get mostRecentReport() { if (this.lastReport) { return this.lastReport @@ -55,11 +63,27 @@ class AccountingDepartment extends Department { this.addReport(value) } - constructor(id: string, private reports: string[]) { + private constructor(id: string, private reports: string[]) { super(id, 'Accounting') this.lastReport = reports[0]; } + // クラスの内部からしかアクセスできない問題を解決するにはstaticメソッドを使ってオブジェクトを作成する + // メソッド名に決まりはない + // インスタンスがあるかどうかをチェックするメソッド + // インスタンスがあれば既存のインスタンスを返す + static getInstance() { + if (this.instance) { + return this.instance; + } + // 以下でもできる + // if (AccountingDepartment.instance) { + // return this.instance; + // } + this.instance = new AccountingDepartment('d2', []); + return this.instance; + } + describe() { console.log('会計部門 - ID: ' + this.id); } @@ -95,8 +119,14 @@ it.describe() it.printEmployeeInformation(); console.log(it); +// AccountingDepartmentのconstructorにprivateをするとnew AccountingDepartmentで以下のようなエラーが表示される +// Constructor of class 'AccountingDepartment' is private and only accessible within the class declaration.ts(2673) +// const accounting = new AccountingDepartment('d2', []); +const accounting = AccountingDepartment.getInstance(); +const accounting2 = AccountingDepartment.getInstance(); + +console.log(accounting, accounting2); -const accounting = new AccountingDepartment('d2', []); accounting.mostRecentReport = 'abc' accounting.addReport('Something') From 497d6de4987ee66ff3d44188ddfab71bbef22ed3 Mon Sep 17 00:00:00 2001 From: toshi-ue Date: Fri, 14 Jun 2024 07:14:17 +0900 Subject: [PATCH 15/15] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AA=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/currentBackup.ts | 126 ------------------------------------------ src/previousBackup.ts | 108 ------------------------------------ 2 files changed, 234 deletions(-) diff --git a/src/currentBackup.ts b/src/currentBackup.ts index c67a321..e69de29 100644 --- a/src/currentBackup.ts +++ b/src/currentBackup.ts @@ -1,126 +0,0 @@ -// // 68. static メソッド & プロパティ -// abstract class Department { -// static fiscalYear = 2020; - -// protected employees: string[] = []; - -// static createEmployee(name: string) { -// return { name: name }; -// } - -// constructor(protected readonly id: string, public name: string) { -// console.log(Department.fiscalYear); -// } - -// // クラス名にもabstractをつけないと以下のようなエラーが表示される -// // Abstract methods can only appear within an abstract class.ts(1244) -// // 解決するにはclassにabstractをつける -// // classにabstractをつけるとdescribeに以下のようなエラーが表示される -// // Method 'describe' cannot have an implementation because it is marked abstract.ts(1245) -// // abstractクラスはメソッドを追加できないということなので以下を行う -// // 中かっこを削除 -// // 戻り値を指定する -// // 以下のコードはabstractを付与して以下を強制している -// // - メソッド名がdescribeであること -// // - thisのオブジェクトはDepartmentクラスかそのサブクラスであること -// // - 戻り値がvoidであること -// abstract describe(this: Department): void; - -// addEmployee(employee: string) { -// this.employees.push(employee); -// } - -// printEmployeeInformation() { -// console.log(this.employees.length); -// console.log(this.employees); -// } -// } - -// // 上でabstractが使われると以下のエラーが発生する -// // Non-abstract class 'ITDepartment' does not implement all abstract members of 'Department'ts(18052) -// // Non-abstract class 'ITDepartment' does not implement inherited abstract member 'describe' from class 'Department'. -// // abstractクラスで定義されているdescribeメソッドを追加してあげればエラーがなくなる -// class ITDepartment extends Department { -// admins: string[]; -// constructor(id: string, admins: string[]) { -// super(id, 'IT') -// this.admins = admins; -// } - -// describe() { -// console.log('IT部門 - ID: ' + this.id); - -// } -// } - -// class AccountingDepartment extends Department { -// private lastReport: string; - -// get mostRecentReport() { -// if (this.lastReport) { -// return this.lastReport -// } - -// throw new Error("レポートが見つかりません。"); -// } - -// 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]; -// } - -// describe() { -// console.log('会計部門 - ID: ' + this.id); -// } - -// addReport(text: string) { -// this.reports.push(text) -// this.lastReport = text; -// } - -// printReports() { -// console.log(this.reports); -// } - -// addEmployee(name: string) { -// if (name === 'Max') { -// return -// } -// this.employees.push(name); -// } -// } - -// const employee1 = Department.createEmployee('Max') -// console.log(employee1, Department.fiscalYear); - - -// const it = new ITDepartment('d1', ['Max']); - - -// it.addEmployee('Max') -// it.addEmployee('Manu') - -// it.describe() -// it.printEmployeeInformation(); - -// console.log(it); - -// const accounting = new AccountingDepartment('d2', []); - -// accounting.mostRecentReport = 'abc' -// accounting.addReport('Something') -// console.log(accounting.mostRecentReport); -// accounting.printReports() - -// accounting.addEmployee('Max') -// accounting.addEmployee('Manu') - -// accounting.describe() -// accounting.printEmployeeInformation(); diff --git a/src/previousBackup.ts b/src/previousBackup.ts index 0ad8781..e69de29 100644 --- a/src/previousBackup.ts +++ b/src/previousBackup.ts @@ -1,108 +0,0 @@ -// // 68. static メソッド & プロパティ -// class Department { -// // 静的プロパティを追加 -// static fiscalYear = 2020; - -// protected employees: string[] = []; - -// // 静的メソッドを追加 -// static createEmployee(name: string) { -// return { name: name }; -// } - -// // constructorはstaticにすることができない -// constructor(private readonly id: string, public name: string) { -// // staticでないところ(static修飾子が付与されていないところ)からはアクセスできない -// // console.log(this.fiscalYear); -// // もしアクセスしたいなら以下のように書く -// // console.log(Department.fiscalYear); -// } - -// describe(this: Department) { -// console.log(`Department (${this.id}): ${this.name}`); -// } - -// addEmployee(employee: string) { -// this.employees.push(employee); -// } - -// printEmployeeInformation() { -// console.log(this.employees.length); -// console.log(this.employees); -// } -// } - -// class ITDepartment extends Department { -// constructor(id: string, private admins: string[]) { -// super(id, 'IT') -// this.admins = admins; -// } -// } - -// class AccountingDepartment extends Department { -// private lastReport: string; - -// get mostRecentReport() { -// if (this.lastReport) { -// return this.lastReport -// } - -// throw new Error("レポートが見つかりません。"); -// } - -// 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; -// } - -// printReports() { -// console.log(this.reports); -// } - -// addEmployee(name: string) { -// if (name === 'Max') { -// return -// } -// this.employees.push(name); -// } -// } - -// const employee1 = Department.createEmployee('Max') -// console.log(employee1, Department.fiscalYear); - - -// const it = new ITDepartment('d1', ['Max']); - - -// it.addEmployee('Max') -// it.addEmployee('Manu') - -// it.describe() -// it.printEmployeeInformation(); - -// console.log(it); - -// const accounting = new AccountingDepartment('d2', []); - -// accounting.mostRecentReport = 'abc' -// accounting.addReport('Something') -// console.log(accounting.mostRecentReport); -// accounting.printReports() - -// accounting.addEmployee('Max') -// accounting.addEmployee('Manu') - -// accounting.describe() -// accounting.printEmployeeInformation();