λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

πŸ’¬/γ…γ……γ…Œγ…‹γ…γ…… μ±Œλ¦°μ§€

27일차

❀️‍πŸ”₯

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

 

'πŸ’¬ > γ…γ……γ…Œγ…‹γ…γ…… μ±Œλ¦°μ§€' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

29일차  (0) 2022.03.17
28일차  (0) 2022.03.17
26일차  (0) 2022.03.16
25일차  (0) 2022.03.15
24일차  (0) 2022.03.07