this.firstName = firstName
์ด๋ฐ ์ฝ๋๋ฅผ ๋ฃ์ด์คฌ์(์๋ ํ๋ฉด ์ฐธ๊ณ )
class Player {
constructor(
private firstName: string,
private lastName: string,
public nickname: string
) {}
}
// js์ private, public์ด ์๋ํ์ง ์์
const nico = new Player("nico", "las", "๋๊ผฌ")
// nico.firstName // private์ด๋ผ์ ์ปดํ์ผ ์ค๋ฅ
abstract class User {
constructor(
protected firstName: string,
protected lastName: string,
protected nickname: string
) {}
// ์ถ์ ๋ฉ์๋
abstract getNickName(): void
// ๊ธฐ๋ณธ ๋ฉ์๋ : firstName + lastName์ ํ ๋ฌธ์์ด๋ก ์ถ๋ ฅํด์ค
getFullName(){
return `${this.firstName} ${this.lastName}`
}
}
class Player extends User {
getNickName() {
console.log(this.nickname)
}
}
const nico = new Player("nico", "las", "๋๊ผฌ");
nico.getFullName()
nico.getNickName() // User ํด๋์ค ์์๋ฐ์์ผ๋ฏ๋ก ๋ฉ์๋ ํธ์ถ ๊ฐ๋ฅ
// console.log(nico.nickname) // protected๋ผ์ ํด๋์ค ๋ฐ์์ ์ ๊ทผ ๋ถ๊ฐ
์คํ ๊ฒฐ๊ณผ
// Words ํ์
: string๋ง์ property๋ก ๊ฐ์ง๋ object์
// ์๋์ ๋ฌธ๋ฒ์ object์ ํ์
์ ์ ์ธํ ๋ ์ฌ์ฉ
// object๊ฐ ์ ํ๋ ๊ฐ์์ property ๋๋ key๋ฅผ ๊ฐ์ง๋๋ก ์ ์ํด์ฃผ๋ ๋ฐฉ๋ฒ
type Words = {
[key: string]: string
}
class Dict {
// words ํ๋กํผํฐ๊ฐ ์์ฑ์๋ถํฐ ๋ฐ๋ก ์ด๊ธฐํ๋์ง ์๊ณ ๋ด๊ฐ ์ํ๋ ๋๋ก ์๋์ผ๋ก ์ด๊ธฐํ์ํด
private words: Words
constructor(){
this.words = {}
}
// ์ ๋จ์ด ์ถ๊ฐ ๋ฉ์๋
add(word: Word){// ํ๋ผ๋ฏธํฐ๊ฐ ์ด๋ค ํด๋์ค์ ์ธ์คํด์ค์ด๊ธธ ์ํ๋ฉด, ํ๋ผ๋ฏธํฐ์ ํด๋์ค๋ฅผ ํ์
์ฒ๋ผ ์ฌ์ฉ ๊ฐ๋ฅํจ
// ์ฃผ์ด์ง ๋จ์ด๊ฐ ์์ง ์ฌ์ ์ ์กด์ฌํ์ง ์์ผ๋ฉด
if(this.words[word.term] === undefined){
this.words[word.term] = word.def
}
}
// ๋จ์ด ์ญ์ ๋ฉ์๋
// ๋จ์ด ์
๋ฐ์ดํธ ๋ฉ์๋
// term์ ์ด์ฉํด์ def๋ฅผ ์ฐพ๋ ๊ธฐ๋ฅ
def(term: string){
return this.words[term]
}
}
class Word {
constructor(
public readonly term: string,
public readonly def: string
) {}
// ๋จ์ด์ ์ ์ ์ถ๊ฐ ๋ฉ์๋
// ๋จ์ด์ ์ ์ ์์ ๋ฉ์๋
// ๋จ์ด ์ถ๋ ฅ ๋ฉ์๋
}
const kimchi = new Word("kimchi", "ํ๊ตญ์ ์์");
const dict = new Dict()
dict.add(kimchi);
dict.def("kimchi");
์ธํฐํ์ด์ค๋ ํ์ ๊ณผ ๋น์ทํ์ง๋ง ํ์ ์ด ๋ ๋ง์ ์ฉ๋๋ก ์ฐ์
< ํ์ >
// ์ฌ๊ธฐ์ string ํ์
์ ํน์ ๊ฐ๋ค์ธ "red" | "blue" | "yellow"
type Team = "red" | "blue" | "yellow"
type Health = 1 | 5 | 10
type Player = {
nickname: string,
team: Team
health: Health
}
const nico: Player = {
nickname: "nico",
team: "red"
health: 1
}
< ์ธํฐํ์ด์ค >
์ธํฐํ์ด์ค๋ ์ค์ง 1๊ฐ์ง ์ฉ๋ ๊ฐ์ง
: object์ ๋ชจ์์ ์ ํด์ค
=> ์ด ์ฉ๋๋ก ์ธ ๋ ํ์
๋ณด๋ค ์ธํฐํ์ด์ค๋ฅผ ์ฐ๋ ๊ฑธ ์ถ์ฒํจ (OOP์ฒ๋ผ ๋ณด์ฌ์ ๊ฐ๋
์ฑ ๊ตฟ)
// ์์ Player ํ์
์ฝ๋์ ๋์ผํ ๋ป์
interface Player {
nickname: string,
team: Team
health: Health
}
์ธํฐํ์ด์ค๋ ํด๋์ค์ ๋ฎ์์
์์๋ฅผ ํตํด ์์๋ณด์
๋์ผํ ๋ด์ฉ์ ์ฝ๋๋ฅผ ์์์ ์ธํฐํ์ด์ค๋ก ๊ตฌํํ๊ณ , ์๋์์ ํ์
์ผ๋ก ๊ตฌํํ๋ค
์ธํฐํ์ด์ค๋ฅผ ์์ํ๋ ๊ฑด extends๋ก, type์ ์์ํ๋ ๊ฑด & ์ฐ์ฐ์๋ฅผ ์ด์ฉํ๋ค
interface User {
name: string
}
interface Player extends User {
}
const nico: Player = {
name: "nico"
}
type User = {
name: string
}
type Player = User & {
}
const nico: Player = {
name: "nico"
}
interface User {
name: string
}
interface User {
lastName: string
}
interface User {
health: number
}
// User ์ธํฐํ์ด์ค 3๊ฐ๊ฐ ํฉ์ณ์ ธ์ nico ๊ฐ์ฒด๋ nico.name, nico.lastName, nico.health ํ๋กํผํฐ๋ฅผ ๋ชจ๋ ๊ฐ์ง
const nico: User = {
name: "nico",
lastName: "n",
health: 1
}