[노마드코더] Typescript(3)

Heechang Jeong·2023년 4월 30일
0

Typescript

목록 보기
3/7
post-thumbnail

✍ 복습 자료

  • 추상 클래스

    오직 다른 곳에서 상속받을 수만 있는 클래스이다. => 직접적으로 인스턴스를 만들지는 못한다는 의미.
  • Cannot create an instance of an abstract class.

abstract class User {
    constructor(
        private firstName: string,
        private lastName: string,
        public nickname: string
    ) {}
}

class Player extends User {

}

const nico = new User("nico", "las", "니꼬");
  • 추상 클래스 안의 메서드

abstract class User {
    constructor(
        private firstName: string,
        private lastName: string,
        public nickname: string
    ) {}
    getFullName() {
        return `${this.firstName} ${this.lastName}`
    }
}

class Player extends User {

}

const nico = new Player("nico", "las", "니꼬");
nico.getFullName()


  • 추상 메서드

    추상 클래스 안에서는 추상 메서드를 만들 수 있다.
    하지만 메서드를 구현하면 안 된다. 그 대신에 메서드의 call signature만 작성해야 함
abstract class User {
    constructor(
        protected firstName: string,
        protected lastName: string,
        protected nickname: string
    ) {}
    // 추상 메서드 : 구현되어 있지 않은 메서드
    abstract getNickname():void
    getFullName() {
        return `${this.firstName} ${this.lastName}`
    }
}

class Player extends User {
    getNickname() {
        console.log(this.nickname)
    }
}

const nico = new Player("nico", "las", "니꼬");
nico.getFullName()
  • private, protected

    private : 인스턴스 밖에서 접근할 수 없고, 다른 자식 클래스에서도 접근할 수 없다.
    protected : 클래스 밖에서는 접근할 수 없다.

  • Property 'getFullName' is private and only accessible within class 'User'.

    private, public은 property 뿐만 아니라 메서드에서도 작동한다

abstract class User {
    constructor(
        private firstName: string,
        private lastName: string,
        public nickname: string
    ) {}
    private getFullName() {
        return `${this.firstName} ${this.lastName}`
    }
}

class Player extends User {

}

const nico = new Player("nico", "las", "니꼬");
nico.getFullName()
  • Javascript에서 작동하는 모습

    abstract는 보이지 않고 일반적인 클래스
"use strict";
class User {
    constructor(firstName, lastName, nickname) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.nickname = nickname;
    }
    getFullName() {
        return `${this.firstName} ${this.lastName}`;
    }
}
class Player extends User {
}
const nico = new Player("nico", "las", "니꼬");
nico.getFullName();


  • 사전 만들기

type Words = {
    [key:string]: string
}

class Dict {
    // words가 private이므로 dictionary 안에서만 words를 보기 원한다.
    private words: Words
    constructor() {
    	// 수동으로 초기화 시켜줬음
        this.words = {}
    }
    // 클래스를 타입처럼 사용함
    add(word:Word) {
    	// 주어진 단어가 아직 사전에 존재하지 않을 때
        if(this.words[word.term] === undefined) {
            this.words[word.term] = word.def;
        }
    }
    def(term:string) {
        return this.words[term]
    }
}

class Word {
    constructor(
        public term:string,
        public def: string
    ) {}
}

const kimchi = new Word("kimchi", "한국의 음식");

const dict = new Dict()

dict.add(kimchi);
dict.def("kimchi")

0개의 댓글