extends
키워드를 사용하여 상위 클래스를 상속 받음class Department {
private employees: string[] = [];
constructor(private readonly id: string, public name: string) {
this.name = n;
}
describe(this: Department) {
console.log('pDepartment: ' + this.name);
}
addEmployee(employee: string) {
this.employees.push(employee);
}
printEmployeeInfo() {
console.log(this.employees.length);
console.log(this.employees);
}
}
class ITDepartment extends Department {}
const accounting = new ITDepartment('d1', 'accounting')
super
를 호출해서 실행해야함class ITDepartment extends Department {
constructor(id: string, public admins: string[]) {
super(id, 'IT');
this.admins = admins;
}
}
const it = new ITDepartment('d2', ['Max']);
protected
로 선언해야함class Department {
// private을 protected로 변경함
protected employees: string[] = [];
constructor(private readonly id: string, public name: string) {
}
addEmployee(employee: string) {
this.employees.push(employee);
}
}
class AccountingDepartment extends Department {
constructor(id: string, private reports: string[]) {
super(id, 'Accounting');
}
// Department의 addEmployee 메소드를 overriding함
addEmployee(name: string) {
if (name === 'Max') {
return;
}
this.employees.push(name);
}
}
get
getterName() { return value; }class AccountingDepartment extends Department {
private lastReport: string;
get mostRecentReport() {
if (this.lastReport) {
return this.lastReport;
}
throw new Error('No report found.');
}
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', []);
// Error, reports가 빈 배열이므로
console.log(accounting.mostRecentReport);
accounting.addReport('Something went wrong...');
// OK, 'Something went wrong...'
console.log(accounting.mostRecentReport);
set
setterName(value) {}class AccountingDepartment extends Department {
private lastReport: string;
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];
}
addReport(text: string) {
this.reports.push(text);
this.lastReport = text;
}
}
const accounting = new AccountingDepartment('d2', []);
// Error, ''는 false이기 때문에
accounting.mostRecentReport = '';
// OK, 'Year End Report'가 this.reports에 추가됨
accounting.mostRecentReport = 'Year End Report';
static
methodName(params) { return MappedObject; }static
variableName = value;class Department {
static fiscalYear = 2020;
protected employees: string[] = [];
constructor(private readonly id: string, public name: string) {
this.name = n;
}
static createEmployee(name: string) {
return {name: name};
}
}
const employee1 = Department.createEmployee('Max');
console.log(employee1) // { name: 'Max'}
console.log(Department.fiscalYear) // 2020
this
키워드를 사용하여 접근할 수 없음abstract
키워드를 붙임abstract class Department {
static fiscalYear = 2020;
protected employees: string[] = [];
constructor(private readonly id: string, public name: string) {
this.name = n;
}
abstract describe(this: Department): void;
}
class ITDepartment extends Department {
constructor(id: string, public admins: string[]) {
super(id, 'IT');
this.admins = admins;
}
describe() {
console.log(`IT Department: ${this.id}`);
}
}
private
키워드를 붙여서 만듬class AccountingDepartment extends Department {
private lastReport: string;
private static instance: AccountingDepartment;
private constructor(id: string, private reports: string[]) {
super(id, 'Accounting');
this.lastReport = reports[0];
}
static getInstance() {
if (AccountingDepartment.instance) {
return this.instance;
}
this.instance = new AccountingDepartment('d2', []);
return this.instance;
}
}
// Error
const accounting = new AccountingDepartment('d2', []);
// OK
const accounting = AccountingDepartment.getInstance();
const accounting1 = AccountingDepartment.getInstance();
console.log(accounting === accounting1) // true
interface
는 객체의 구조를 설명함interface
에서는 값을 초기화할 수 없음interface Person {
name: string;
age: number;
greet(phrase: string): void;
}
let use1: Person;
user1 = {
name: 'Max',
age: 40,
greet: (phrase: string) {
console.log(phrase + ' ' + this.name);
}
}
user1.greet('Hi there, I am') // 'Hi there, I am Max'
interface
를 준수하는 클래스는 interface
와 같은 구조를 가짐interface
를 준수하는 클래스 생성 방법 : Class ClassName implements
interfaceName {}interface Greetable {
name: string;
greet(phrase: string): void;
}
Class Person implements Greetable {
name: string;
constructor(n: string) {
this.name = n;
}
greet(phrase: string) {
console.log(phrase + ' ' + this.name);
}
}
interface
를 준수하는 클래스에 메소드나 필드를 추가 가능함interface Greetable {
name: string;
greet(phrase: string): void;
}
Class Person implements Greetable {
name: string;
age: number = 30;
constructor(n: string) {
this.name = n;
}
greet(phrase: string) {
console.log(phrase + ' ' + this.name);
}
}
const user1 = new Person('Max');
console.log(user1); // Person {age: 30, name: "Max"}
interface
는 구체적인 구현이 아니라 서로 다른 클래스 간의 기능을 공유하기 위해 사용됨
interface
를 타입으로 정의할 수 있음
interface Greetable {
name: string;
greet(phrase: string): void;
}
Class Person implements Greetable {
name: string;
age: number = 30;
constructor(n: string) {
this.name = n;
}
greet(phrase: string) {
console.log(phrase + ' ' + this.name);
}
}
let user1 : Greetable;
const user1 = new Person('Max');
console.log(user1); // Person {age: 30, name: "Max"}
interface
에 readonly
키워드를 추가할 수 있음 (public
, private
은 지정 불가능)readonly interface Greetable {
name: string;
greet(phrase: string): void;
}
Class Person implements Greetable {
name: string;
age: number = 30;
constructor(n: string) {
this.name = n;
}
greet(phrase: string) {
console.log(phrase + ' ' + this.name);
}
}
let user1 : Greetable;
const user1 = new Person('Max');
console.log(user1); // Person {age: 30, name: "Max"}
readonly
키워드 추가 가능함interface Greetable {
readonly name: string;
greet(phrase: string): void;
}
Class Person implements Greetable {
name: string;
age: number = 30;
constructor(n: string) {
this.name = n;
}
greet(phrase: string) {
console.log(phrase + ' ' + this.name);
}
}
const user1 = new Person('Max');
// Error, Greatable interface로부터 name 속성이 readonly라는 것을 추론
user1.name = 'Manu';
extends
키워드를 사용하여 interface
를 상속함interface Named {
readonly name: string;
}
interface Greetable extends Named {
greet(phrase: string): void;
}
Class Person implements Greetable {
name: string;
age: number = 30;
constructor(n: string) {
this.name = n;
}
greet(phrase: string) {
console.log(phrase + ' ' + this.name);
}
}
const user1 = new Person('Max');
// Error, Greatable interface로부터 name 속성이 readonly라는 것을 추론
user1.name = 'Manu';
interface
는 쉼표(,)로 구분하여 한 클래스에 여러 개 interface
를 합칠 수 있음interface Greetable {
name: string;
greet(phrase: string): void;
}
Class Person implements Greetable, AnotherInterface {
...
}
interface
interfaceName { {params: paramsType}: returnType }interface AddFn {
{a:number, b:number}: number;
}
let add: AddFn;
add = (n1: number, n2: number) => {
return n1 + n2;
}
?
)를 추가하여 선택적 속성을 지정할 수 있음interface Named {
readonly name: string;
outputName?: string;
}
Class Person implements Greetable {
// outputName 없어도 no Error
name: string;
age: number = 30;
constructor(n: string) {
this.name = n;
}
greet(phrase: string) {
console.log(phrase + ' ' + this.name);
}
}
interface Named {
readonly name?: string;
}
Class Person implements Greetable {
name?: string;
age: number = 30;
constructor(n?: string) {
if (n) {
this.name = n;
}
}
greet(phrase: string) {
if (this.name) {
console.log(phrase + ' ' + this.name);
} else {
console.log('Hi');
}
}
}
// No Error
const user1 = new Person();