β€οΈπ₯
27μΌμ°¨
Part 4. TypeScript Essentials
Ch 6. Classes
Ch 6. Classes (ES6~)
classλ objectλ₯Ό λ§λλ μ²μ¬μ§μ΄λ€.
// Quick Start
class Person {
name;
constructor(name: string) {
this.name = name;
}
}
const p1 = new Person("Mark");
console.log(p1);
π Constructor & Initialize
class Person {
name: string = "Mark"; // λ°©λ² 1) λν΄νΈ μμ±μ
age!: number; // νλ‘νΌν°λ₯Ό μ μΈνλ κ³³ λλ μμ±μμμ κ°μ ν λΉνμ§ μλ κ²½μ°μλ !λ₯Ό λΆμ¬μ μνμ νννλ€.
// λ°©λ² 2) μμ±μ ν¨μ
constructor(age?: number) {
if (age === undefined) {
this.age = 20;
} else {
this.age = age;
}
}
async init() {}
}
const p1: Person = new Person(39);
const p2: Person = new Person();
console.log(p1);
console.log(p2);
π μ κ·Ό μ μ΄μ (Access Modifiers)
- public: λͺ¨λ κ³³μμ μ κ·Όν μ μλ€. default
- private: ν΄λμ€ μΈλΆμμ μ κ·Όν μ μλ€.
- protected: μΈλΆμμ μ κ·Όν μ μμ§λ§, μμ κ΄κ³μ μλ κ³³μμλ μ κ·Όν μ μλ€.
π Initialization In Constructor Parameters
class Person {
public constructor(public name: string, protected age: number) {}
}
const p1: Person = new Person("Mark", 39);
console.log(p1);
π Getters & Setters
class Person {
public constructor(private _name: string, private age: number) {}
get name() {
return this._name + "Lee";
}
set name(n: string) {
this._name = n;
}
}
const p1: Person = new Person("Mark", 39);
console.log(p1.name); // get μ νλ ν¨μ getter
p1.name = "Woongjae"; // set μ νλ ν¨μ setter
console.log(p1.name); // get μ νλ ν¨μ getter
π Readonly Properties
class Person {
public readonly name: string = "Mark";
private readonly contry: string;
public constructor(private _name: string, private age: number) {
this.contry = "Korea"; // Error X
}
hello() {
this.contry = "China"; // Error // read only μ΄κΈ° λλ¬Έμ λ³κ²½ λΆκ°
}
}
const p1: Person = new Person("Mark", 39);
console.log(p1.name);
p1.name = "Woongjae"; // Error // read only μ΄κΈ° λλ¬Έμ λ³κ²½ λΆκ°
console.log(p1.name);
π Index Signatures In Class => λμ μΌλ‘ νλ‘νΌν° μ΄λ¦μ΄ μ¬μ©λ λ μ¬μ©
// class => object
// {mark: 'male', jade: 'male'}
// {chloe: 'female', alex: 'male', anna: 'female'}
class Students {
[index: string]: "male" | "female";
}
const a = new Students();
a.mark = "male";
a.jade = "male";
console.log(a);
const b = new Students();
b.chloe = "female";
b.alex = "male";
b.anna = "female";
console.log(b);
π Static Properties & Methods => 'static' ν€μλλ₯Ό λΆμ¬μ νλ‘νΌν°λ λ©μλλ₯Ό ν΄λμ€μμ μ¬μ©ν μ μλ€.
class Person {
private static CITY = "Seoul";
public hello() {
console.log("μλ
νμΈμ", Person.CITY);
}
public change() {
Person.CITY = "LA";
}
}
const p1 = new Person();
p1.hello();
const p2 = new Person();
p2.hello();
p1.change();
p2.hello();
π Singletons
class ClassName {
private static instance: ClassName | null = null;
public static getInstance(): ClassName {
// ClassName μΌλ‘λΆν° λ§λ objectκ° μμΌλ©΄, λ§λ λ€.
if (ClassName.instance === null) {
ClassName.instance = new ClassName();
}
return ClassName.instance;
}
private constructor() {}
}
const a = ClassName.getInstance();
const b = ClassName.getInstance();
console.log(a === b); // true
π μμ(Inheritance)
class Parent {
constructor(protected _name: string, private _age: number) {}
public print(): void {
console.log(`μ΄λ¦μ ${this._name} μ΄κ³ , λμ΄λ ${this._age} μ
λλ€.`);
}
protected printName(): void {
console.log(this._name, this._age);
}
}
const p = new Parent("Mark", 39);
p.print();
class Child extends Parent {
// μΆκ°
public gender = "male";
constructor(age: number) {
super("Mark Jr", age); // β super(): λΆλͺ¨ μμ±μ νΈμΆ // 맨 μ²μμ νΈμΆ ν΄μΌ ν¨
this.printName();
}
}
const c = new Child(5);
c.print();
π Abstract Classes => μμ νμ§ μμ classλ₯Ό ννν μ μλ€.
abstract class AbstractPerson {
protected _name: string = "Mark";
abstract setName(name: string): void; // abstractλ‘ μμ νμ§ μμ classλ₯Ό ννν μ μλ€.
}
// new AbstractPerson(); // π¨ Error // classκ° μμ νμ§ μκΈ° λλ¬Έμ μ€νν μ μλ€.
class Person extends AbstractPerson {
setName(name: string): void {
this._name = name;
console.log(this._name);
}
}
const p = new Person();
p.setName("Hong"); // Hong