diff --git a/src/app.ts b/src/app.ts index d8a50ee..a66cd88 100644 --- a/src/app.ts +++ b/src/app.ts @@ -2,11 +2,19 @@ class Department { // private id: string; // private name: string; private employees: string[] = []; - // readonly는 프로퍼티를 초기화한 후 수정할 수 없다. 즉, 한번 할당 되면 변경되면 안되는 고유 번호들을 설정할 때 readonly를 사용한다. + static fiscalYear = 2020; + constructor(private readonly id: string, public name: string) { // this.id = id; // this.name = n + // console.log(this.fiscalYear); //Error: static은 생성자가 필요 없기 때문에 Error 발생 즉, 생성자 함수, 클래스 내부에서는 static으로 선언된 정적인 함수, 변수는 재정의나 활용이 불가능 + //만약 접근을 하려면 해당 클래스를 명시하고 접근을 해야 정상동작 + console.log(Department.fiscalYear); //정상동작 + } + static createEmployee(name: string) { + return { name: name }; } + describe(this: Department) { console.log(`Department (${this.id}): ${this.name}`); } @@ -29,6 +37,21 @@ class ITDepartment extends Department { this.admins = admins; } } +const accounting = new Department('1', 'Accounting'); +const ITaccounting = new ITDepartment('2', ['Max']); + +ITaccounting.addEmployee('Max'); +ITaccounting.addEmployee('Manu'); + +// accounting.employees[2] = 'Anna'; +ITaccounting.describe(); +ITaccounting.printEmployeeInformation(); + +const NewAccounting = new AccountingDepartment('d2', []); + +// console.log(NewAccounting.mostRecentReport); //report가 추가되지 않아서 Error +NewAccounting.setMostRecentReport = 'Year End Report'; +NewAccounting.addReport('Something went wrong...'); class AccountingDepartment extends Department { private lastReport: string; @@ -45,13 +68,12 @@ class AccountingDepartment extends Department { throw new Error('Please pass in a valid value!') } this.addReport(value); - this.lastReport = value; // 여기서 lastReport를 업데이트 그래야 lastReport가 비어있지 않기 때문에 정상적으로 동작을 한다. + this.lastReport = value; } constructor(id: string, private reports: string[]) { super(id, 'Account'); - //strictPropertyInitialization 활성화로 초기화 해줘야 함. - this.lastReport = reports[0] || ""; // 초기값을 할당 (reports가 비어있으면 빈 문자열) + this.lastReport = reports[0] || ""; } addReport(text: string) { @@ -62,9 +84,15 @@ class AccountingDepartment extends Department { console.log(this.reports); } } + const accounting = new Department('1', 'Accounting'); const ITaccounting = new ITDepartment('2', ['Max']); +Math.pow(1, 2); //new, 인스턴스를 사용하지 않고 바로 사용할 수 있는 클래스 Math가 있다. 이와 같이 우리의 class도 구현 해볼 것이다. 방법은 static을 사용하는 것이다. + +const employees1 = Department.createEmployee('Max'); //정적 메소드 불러오는 방식 +console.log(employees1, Department.fiscalYear); + ITaccounting.addEmployee('Max'); ITaccounting.addEmployee('Manu'); @@ -74,10 +102,10 @@ ITaccounting.printEmployeeInformation(); const NewAccounting = new AccountingDepartment('d2', []); -// console.log(NewAccounting.mostRecentReport); //report가 추가되지 않아서 Error +// console.log(NewAccounting.mostRecentReport); NewAccounting.setMostRecentReport = 'Year End Report'; NewAccounting.addReport('Something went wrong...'); -console.log(NewAccounting.mostRecentReport); //report가 있어서 문제없이 출력 +console.log(NewAccounting.mostRecentReport); NewAccounting.printReports();