//1번. 인터페이스는 타입만 가지고 있는 반면 클래스는 실제 값과 타입을 모두 가질 수 있다.
//2번.상속은 가능하지만 인스턴스화 할 수 없다. 상속받은 클래스 또한 인스턴스화 할 수 없다.
class testA {
protected constructor(
public Name: string
) { }
}
const test1 = new testA("name1");//Constructor of class 'testA' is protected and only accessible within the class declaration.
class testB extends testA {}
const test2 = new testB("name2")//Constructor of class 'testA' is protected and only accessible within the class declaration.
//3번.
type S = {
purpose:string
}
class A implements S {
purpose = "A"
}
class B implements S {
purpose = "B"
}
class C implements S {
purpose = "C"
}
type SC = {
create(type: 'a'):A
create(type: 'b'):B
create(type: 'c'):C
}
let S:SC = {
create(type: 'a' | 'b' | 'c'): S {
switch (type) {
case 'a': return new A
case 'b': return new B
case 'c': return new C
}
}
}
let t = S.create('a')// A
//4번
4번 문제는 다소 난해하게 느껴져서 나중에 따로 풀어봐야겠다.
풀이에서는 다른 클래스를 여러개 만들고, 메소드 호출 시 다른 클래스를 반환하는 방식으로 구현이 됐던데 다소 난잡하게 느껴졌다.
728x90
'Typescript > 타입스크립트 프로그래밍 연습문제' 카테고리의 다른 글
7장 연습문제 (0) | 2023.03.30 |
---|---|
6장 연습문제 (1) | 2023.03.29 |
4장 연습문제 (0) | 2023.03.22 |
3장 연습문제 (0) | 2023.03.21 |