공부기록/웹 개발

[타입스크립트] class

_우지 2022. 10. 5. 19:18

Constructor 가 있을 때 Class

class A {
  a: string;
  b: number;
  constructor(a: string, b: number = 123) {
    this.a = a;
    this.b = b;
  }
  method() {}
}

const a = new A("123");
console.log(a); // A { a: '123', b: 123 }
const b = new A("DONG", 1006);
console.log(b); // A { a: 'DONG', b: 1006 }

class 이름을 type 으로 사용할 수 있다.

이때는 new 키워드를 사용하여 instance 를 할당해주어야한다.

const aaa: A = new A("123");

class 자체를 할당하려면 type 은 어떻게 선언해주어야할까?

이때는 new 키워드를 사용하여 instance 를 할당해주어야한다.

const bbb: typeof A = A

interface, implements

class에 implements를 선언하면 implements 로 선언된 interface 를 따라야한다.

interface A {
  readonly a: string;
  b: string;
}

class B implements A {
  a: string = "123";
  b: string = "hello";
}

private, protected, public

constructor가 있을 때

private 와 protected 예약어를 사용할때에 interface를 implements 할 경우에 오류가 발생했다. 따라서 private 와 protected 를 사용하는 interface 의 타입을 제거해주었다.

interface Employee {
  c: string;
}

class Developer implements Employee {
    constructor(private a: string, protected b: string, public c: string) {
      this.a = a;
      this.b = b;
      this.c = c;
    }

  method() {
    console.log(this.a);
    console.log(this.b);
    console.log(this.c);
  }
}

class C extends Developer {
  method() {
    console.log(this.a); // 접근 불가능
    console.log(this.b); 
    console.log(this.c);
  }
}

new C("zero", "cho", "world").a; // 접근 불가능
new C("zero", "cho", "world").b; // 접근 불가능
new C("zero", "cho", "world").c;

constructor가 없을 때

constructor가 없는 경우도 마찬가지이다.

interface Employee {
  c: string;
}

class Developer implements Employee {
  private a: string = "hello";
  protected b: string = "hhl";
  c: string = "hihihi";

  method() {
    console.log(this.a);
    console.log(this.b);
    console.log(this.c);
  }
}

class C extends Developer {
  method() {
    console.log(this.a); // 접근 불가능
    console.log(this.b);
    console.log(this.c);
  }
}

new C().a; // 접근 불가능
new C().b; // 접근 불가능
new C().c;

implements를 제거해주었을 때

객체 지향의 추상화를 꼭 사용하지 않아도 된다면 아래처럼 implements 없이 class 를 구현하는 것이 좀 더 나은 것 같다. interface 를 만들지 않아도 되기 때문이다.

class Developer {
  private a: string = "hello";
  protected b: string = "hhl";
  c: string = "hihihi";

  method() {
    console.log(this.a);
    console.log(this.b);
    console.log(this.c);
  }
}

class C extends Developer {
  method() {
    console.log(this.a); // 접근 불가능
    console.log(this.b);
    console.log(this.c);
  }
}

new C().a; // 접근 불가능
new C().b; // 접근 불가능
new C().c;

public , protected, private 가 접근 가능한 class 를 요약하면 다음과 같다.

            Public             protected         private
클래스내부         O                  O              O
인스턴스        O                  X              X
상속클래스        O                 O              X

abstract

abstract 를 사용하면 추상화를 사용할 수 있다.
아래의 class C는 Developer 를 extends 하였기 때문에 abstract인 method 를 반드시 구현해야한다.

 abstract class Developer {
  private a: string = "hello";
  protected b: string = "hhl";
  c: string = "hihihi";

  abstract method(): void;
}

class C extends Developer {}