class Department {
private id: string;
private name: string;
private employees: string[] = [];
constructor(id: string, name: string) {
this.id = id;
this.name = n;
}
}
위를 축약할 수 있다.
class Department {
// private id: string;
// private name: string;
private employees: string[] = [];
constructor(private id: string, public name: string) {
// this.id = id;
// this.name = n;
}
}
class Department {
// private id: string;
// private name: string;
private employees: string[] = [];
constructor(private readonly id: string, public name: string) {
// this.id = id;
// this.name = n;
}
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, public admins: string[]) {
super(id, "IT");
}
}
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);
}
}
const it = new ITDepartment("d1", ["Max"]);
it.addEmployee("Max");
it.addEmployee("Manu");
// it.employees[2] = 'anna';
it.describe();
it.printEmployeeInformation();
console.log(it);
const accounting = new AccountingDepartment('d2', []);
accounting.addReport('something went wrong...');
accounting.printReports();
private는 해당 클래스에서만 접근 가능
protected 상속한 클래스까지 접근 가능
public는 모두 접근 가능
class Department {
// private id: string;
// private name: string;
protected employees: string[] = [];
constructor(private readonly id: string, public name: string) {
// this.id = id;
// this.name = n;
}
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 AccountingDepartment extends Department {
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 pass in a valid 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;
}
printReports() {
console.log(this.reports);
}
}
const accounting = new AccountingDepartment("d2", []);
accounting.mostRecentReport = 'Year End Report';
accounting.addReport("something went wrong...");
console.log(accounting.mostRecentReport);