let's get IT with DAVINA ๐Ÿ’ป

TypeScript(Classes) ๋ณธ๋ฌธ

DEV_IN/TypeScript

TypeScript(Classes)

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 2. 12. 00:19

Pre-Knowledge

๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(Object Oriented Programming)

⇒ ๊ฐ์ฒด์˜ ์ž…์žฅ์—์„œ ๋ณด๋Š”๊ฒƒ!!!

  • ์‚ฌ๋žŒ์ด ์„ธ๊ณ„๋ฅผ ๋ณด๊ณ  ์ดํ•ดํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ‰๋‚ด ๋‚ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ๋ฒ•๋ก  ์ค‘ ํ•˜๋‚˜

⇒ ํ˜„์‹ค ์„ธ๊ณ„๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ชจ๋ธ์„ ๋งŒ๋“ค ๋•Œ์— ์œ ์šฉํ•˜๋‹ค!

  • “ํด๋ž˜์Šค”๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์˜ ์ฒญ์‚ฌ์ง„์„ ์ด์šฉํ•ด ์ฝ”๋“œ ์ž‘์„ฑ
โญ 4๊ฐ€์ง€ ์ฃผ์š” ๊ฐœ๋…
1. Encapsulation(์บก์Šํ™”) : ๋ฐ์ดํ„ฐ์™€ ๊ธฐ๋Šฅ์„ ํ•˜๋‚˜์˜ ๋‹จ์œ„๋กœ ๋ฌถ๋Š” ๊ฒƒ
2. Inheritance(์ƒ์†) : ๊ธฐ๋ณธ ํด๋ž˜์Šค์˜ ํŠน์ง•์„ ํŒŒ์ƒ ํด๋ž˜์Šค๊ฐ€ ์ƒ์†๋ฐ›๋Š”๋‹ค. (๋ถ€๋ชจ/์ž์‹)
3. Abstraction(์ถ”์ƒํ™”) : ๋‚ด๋ถ€ ๊ตฌํ˜„์€ ์•„์ฃผ ๋ณต์žกํ•œ๋ฐ, ์‹ค์ œ๋กœ ๋…ธ์ถœ๋˜๋Š” ๋ถ€๋ถ„์€ ๋‹จ์ˆœํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค๋Š” ๊ฐœ๋…
4. Polymorphism(๋‹คํ˜•์„ฑ) : ๋˜‘๊ฐ™์€ ๋ฉ”์„œ๋“œ์—ฌ๋„ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„๋œ๋‹ค.
⇒ ๊ฐ์ฒด ๋‚ด์—๋Š” “๋ฐ์ดํ„ฐ์™€ ๊ธฐ๋Šฅ์ด ํ•จ๊ป˜ ์žˆ๋‹ค”๋ผ๋Š” ์›์น™์— ๋”ฐ๋ผ ๋ฉ”์„œ๋“œ์™€ ์†์„ฑ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

<ํด๋ž˜์Šค์™€ ์ธ์Šคํ„ด์Šค>

  • class ⇒ ํ•˜๋‚˜์˜ ๋ชจ๋ธ์ด ๋˜๋Š” ์ฒญ์‚ฌ์ง„(blueprint)
  • instance ⇒ ๊ทธ ์ฒญ์‚ฌ์ง„์„ ๋ฐ”ํƒ•์œผ๋กœ ํ•œ ๊ฐ์ฒด(object)๋ฅผ ๋งŒ๋“œ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํŒจํ„ด

ES6 class๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ •์˜ํ•œ๋‹ค! (์ด์ „์—” funtion์„ ์ผ์—ˆ์Œ)

//ES6//
class Car { 
	constructor (brand, name, color) {} //์ƒ์„ฑ์ž ํ•จ์ˆ˜ -> return ๊ฐ’ ์—†์Œ
}

//instance ์ƒ์„ฑํ•˜๊ธฐ
new avante = new Car ('hyundai', 'avante', 'black'); //๊ฐ๊ฐ์˜ ์ธ์Šคํ„ด์Šค๋Š” Car๋ผ๋Š” ํด๋ž˜์Šค์˜ ๊ณ ์œ ํ•œ ์†์„ฑ๊ณผ, ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค.
new mini = new Car ('bmw', 'mini', 'white');
new beetles = new Car ('volkswagen', 'beetles', 'red');

//์ด์ „ function์„ ์ผ์„๋•Œ//
function ๊ธฐ๊ณ„ (๊ตฌ๋ฉ) {
	this.q = ๊ตฌ๋ฉ;
	this.w = 'snowball';
}

//instance์ƒ์„ฑํ•˜๊ธฐ
var nunu = new ๊ธฐ๊ณ„('consume');
var garen = new ๊ธฐ๊ณ„('strike');
โ“ ํด๋ž˜์Šค์™€ ํ•จ์ˆ˜์˜ ์ฐจ์ด
BOTH = ๊ธฐ๋Šฅ์€ ๊ฐ™๋‹ค! ํ•˜๋‚˜์˜ ๋ถ€๋ชจ ๊ฐœ๋…์ธ ํด๋ผ์Šค๋ฅผ ๋งŒ๋“ค์–ด, ์ด๋กœ ์ธํ•ด ์ƒ์„ฑ๋œ ์ž์‹ ๊ฐ์ฒด๋“ค์„ ๋™์ผํ•œ ํด๋ž˜์Šค ๋‚ด์—์„œ ๋‹ค๋ฅด๊ฒŒ ๊ตฌํ˜„ํ•˜๊ฒŒ๋” ๋งŒ๋“ค์–ด์ค€๋‹ค.

DIFF
1. funciton์€ ES6์ด์ „ ๋ฌธ๋ฒ•์œผ๋กœ classํ‚ค์›Œ๋“œ์™€ ๋‹ค๋ฅด๊ฒŒ ์ƒ์„ฑ์žํ•จ์ˆ˜์ธ constructor์„ ๋”ฐ๋กœ ์ƒ์„ฑํ•ด์ฃผ์ง€ ์•Š๋Š”๋‹ค.
2. hoisting - ํ•จ์ˆ˜ ์„ ์–ธ์€ ํ˜ธ์ด์ŠคํŒ…์ด ๋ฐœ์ƒํ•˜์ง€๋งŒ, ํด๋ž˜์Šค ์„ ์–ธ์€ ํ˜ธ์ด์ŠคํŒ…์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.
3. ํด๋ž˜์Šค ์ƒ์„ฑ์ž๋Š” new์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค!
4. ํด๋ž˜์Šค๋Š” ํ•ญ์ƒ ์—„๊ฒฉ๋ชจ๋“œ๋กœ ์‹คํ–‰๋œ๋‹ค.
5. ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๋Š” ์—ด๊ฑฐํ•  ์ˆ˜ ์—†๋‹ค. for…in์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ˆœํ™˜ํ•  ๋•Œ, ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” prototype ํ”„๋กœํผํ‹ฐ์— ์ง€์ •๋œ ํ•จ์ˆ˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ, class๋Š” key๊ฐ’์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š”๋‹ค.
๋”ฐ๋ผ์„œ, class๋ฅผ ์“ธ ๊ฒฝ์šฐ, ๋ฉ”์„œ๋“œ(ํ•จ์ˆ˜)๋Š” ๋‹ค ๋”ฐ๋กœ ์ผ์ผ์ด ๋นผ์ค˜์•ผํ•œ๋‹ค.
โ“ ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ
์†์„ฑ - ex) ์ž๋™์ฐจ ๋ธŒ๋žœ๋“œ, ์ฐจ ์ด๋ฆ„, ์ƒ‰์ƒ, ํ˜„์žฌ ์—ฐ๋ฃŒ ์ƒํƒœ, ์ตœ๊ณ  ์†๋ ฅ ๋“ฑ
๋ฉ”์„œ๋“œ - ๊ฐ์ฒด์— ๋”ธ๋ฆฐ ํ•จ์ˆ˜ ex) ์—ฐ๋ฃŒ ์ฃผ์ž…, ์†๋ ฅ ์„ค์ •, ์šด์ „ ๋“ฑ

Contructor
⇒ ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฉ”์„œ๋“œ
⇒ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ ์ƒ์„ฑ์ž(default constructor)๊ฐ€ ์ œ๊ณต๋œ๋‹ค.
⇒ ๋ถ€๋ชจ ํด๋ž˜์Šค๊ฐ€ ์žˆ๊ณ , ์ž์‹ ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋ถ€๋ชจ ์ƒ์„ฑ์ž๋ฅผ ๋ถ€๋ฅธ๋‹ค.
⇒ ํด๋ž˜์Šค ๋‚ด์—์„œ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ํ•˜๋‚˜๋งŒ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

extends

  • extends ํ‚ค์›Œ๋“œ๋Š” ํด๋ž˜์Šค๋ฅผ ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ์ž์‹์œผ๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด class ์„ ์–ธ ๋˜๋Š” class์‹์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
class ChildClass extends ParentClass {...}

TypeScript (Classes)

  • TS
class Player {
    constructor(
        private firstName: string, 
        private lastName: string,
        public nickName: string
    ) {}
}

const vini = new Player("vini", "An", "๋น„๋‹ˆ");
vini.nickName //instance ์ƒ์„ฑํ•œ ๊ฑฐ์—์„  public๋งŒ ํ˜ธ์ถœ/์ ‘๊ทผ ๊ฐ€๋Šฅ!!
  • JS
class Player {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.nickName = nickName;
    }
}

Abstract Class

  • ๋‹ค๋ฅธ ํด๋ž˜์Šค๊ฐ€ ์ƒ์†๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค
  • BUT!! ์ง์ ‘ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜๋Š” ์—†๋‹ค!!

Abstract Method

  • abstract class ์—์„  abstract method๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
  • abstract method๋Š” ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” ๋ชจ๋“  ๊ฒƒ๋“ค์ด ๊ตฌํ˜„์„ ํ•ด์•ผํ•˜๋Š” method!
    • ๋‹จ, method๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ์•ˆ๋˜๊ณ ,
    • ๋ฉ”์†Œ๋“œ์˜ call signature๋งŒ ์ ์–ด๋‘ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค!
    • abstract method๋Š” abstract class์—์„œ๊ฐ€ ์•„๋‹ˆ๋ผ ์œ„ ์ž์‹ class์—์„œ ๊ตฌํ˜„ํ•ด์ค˜์•ผํ•จ!
  • ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค๋‚˜, ๋‹ค๋ฅธ ์ž์‹ ํด๋ž˜์Šค์—์„œ๋„ abstract class ๋‚ด์—์„œ ์žˆ๋Š” private property | method๋Š” ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ!
- private ⇒ ๊ทธ ํ•ด๋‹น Class(User) ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
- protected ⇒ abstract class(User)์—์„œ๋ถ€ํ„ฐ ์ƒ์„ฑ๋œ ๋‹ค๋ฅธ ์ž์‹ class = extends๋œ class(Player)์—์„œ ์ด์šฉ ๊ฐ€๋Šฅ
but, instance์—์„ (vini) ์ด์šฉ ๋ถˆ๊ฐ€๋Šฅ
- publicabstract class, ์ž์‹ class, instance์—์„œ ๋‹ค ์ด์šฉ ๊ฐ€๋Šฅ
abstract class User {
    constructor(
        protected firstName: string, //protection level + name + type
        protected lastName: string,
        protected nickName: string
    ) {}
    
    abstract getNickName() : void //abstract method๋Š” abstract class์—์„œ call signature๋งŒ ํ•˜๊ณ (type๋งŒ ์ •์˜ํ•ด์ฃผ๊ณ )
    
    getFullName(){
        return `${this.firstName} ${this.lastName}`
    }
}

class Player extends User{
    getNickName() { //abstract method๋Š” ํ•˜์œ„ ์ž์‹ class์—์„œ ๊ตฌํ˜„ํ•ด์ค˜์•ผํ•จ!
        console.log(this.nickName)
    }
}

const vini = new Player("vini", "An", "๋น„๋‹ˆ");
vini.getFullName()
vini.nickName //โ›”๏ธ 'nickName'์†์„ฑ์€ ๋ณดํ˜ธ๋œ ์†์„ฑ์ด๋ฉฐ 'User'ํด๋ž˜์Šค ๋ฐ ํ•ด๋‹น ํ•˜์œ„ ํด๋ž˜์Šค ๋‚ด์—์„œ๋งŒ ์—‘์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธ ์ด ๋•Œ, protection level์€ JS์—์„  ์•ˆ๋ณด์—ฌ!! โญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธ

Recap

type Words = {
    [key:string]:string //string type๋งŒ์„ ๊ฐ€์ง€๋Š” property๋กœ ๊ฐ€์ง€๋Š” object (property์˜ name์€ ๋ชจ๋ฅด๊ณ  type๋งŒ ๋จผ์ € ์•Œ ๋•Œ ์“ฐ๊ธฐ ์œ ์šฉ)
}

class Dict {
    private words : Words //1. create property
    constructor() { //2. manually initialize property
        this.words = {}
        
    }
    add(word:Word){ //3. ์ƒ์„ฑํ•œ class๋ฅผ type์œผ๋กœ์จ ์ •์˜๊ฐ€๋Šฅ = word๋Š” Wordํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค
        if(this.words[word.term]===undefined){
            this.words[word.term]=word.def;
        }
    }
    def(term:string){
        return this.words[term]
    }
    remove(term:string){
		delete this.words[term]
	}
}

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

const kimchi = new Word("kimchi", "ํ•œ๊ตญ์Œ์‹")

const dict = new Dict()
dict.add(kimchi) //{kimchi : ํ•œ๊ตญ์Œ์‹}
dict.def("kimchi") //"ํ•œ๊ตญ์Œ์‹"

//์ด ์‚ฌ์ „์„ ๋ฐ”๊พธ๊ฒŒ ํ•˜๊ณ  ์‹ถ์ง€ ์•Š์•„!!
kimchi.def="xxx"

//๊ทธ๋Ÿฌ๋ ค๋ฉด ์œ„์— public์„ ์œ ์ง€ํ•˜๋ฉด์„œ readonly๊ธฐ๋Šฅ ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ
//private์ด๋‚˜ protected๋กœ ๋ฐ”๊พธ์ง€ ์•Š๊ณ !
class Word {
    constructor(
        public readonly term:string, 
        public readonly def:string
    ) {}
}

'DEV_IN > TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

โ›”๏ธ ์ž์ฃผ ๋ณผ ์ˆ˜ ์žˆ๋Š” Errors  (0) 2023.02.17
TypeScript(apply)  (2) 2023.02.17
TypeScript(Interface)  (4) 2023.02.16
TypeScript (Function)  (3) 2023.02.09
TypeScript๋ž€? & Types  (0) 2023.02.09
Comments