λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

typescript

TypeScript / 클래슀

Classes (클래슀)

객체λ₯Ό μƒμ„±ν•˜κΈ° μœ„ν•œ ν•˜λ‚˜μ˜ ν…œν”Œλ¦Ώ μ—­ν• λ‘œ, ES6μ—μ„œ λ„μž…λœ 클래슀 문법을 기반으둜 ν•œλ‹€.

μƒμ„±μž, 속성, λ©”μ„œλ“œλ‘œ κ΅¬μ„±λ˜μ–΄μžˆλ‹€.

 

// 클래슀 μ •μ˜
class Continent {
    continentName: string;
    constructor(name: string) {
        this.continentName = name;
    }
    getContinentName() {
        return this.continentName;
    }
}

class Country extends Continent {
    name: string;
    capital: string;

    constructor(continentName: string, name: string, capital: string) {
        super(continentName) // λΆ€λͺ¨λ‘œλΆ€ν„° 상속
        this.name = name;
        this.capital = capital;
    }

    getInfo() {
    	// 상속받은 클래슀의 μš”μ†Œμ— μ ‘κ·Όν•˜λ €λ©΄ getContinentName λ©”μ†Œλ“œλ₯Ό 톡해 μ ‘κ·Όν•œλ‹€
        return `${this.name}, ${this.capital}, ${this.getContinentName()}`;
    }
}

// μΈμŠ€ν„΄μŠ€ 생성
let country = new Country(
    'Asia', 'South Korea', 'Seoul'
);

console.log(country.getInfo()) // "South Korea, Seoul, Asia"

 

 

 

μ ‘κ·Ό μ œμ–΄μž(Access Modifiers)

Typescriptμ—λŠ” 클래슀의 속성과 λ©”μ„œλ“œμ˜ μ ‘κ·Ό λ²”μœ„λ₯Ό μ œμ–΄ν•  수 μžˆλŠ” μ ‘κ·Ό μ œμ–΄μžκ°€ μžˆλ‹€.

 

- μ’…λ₯˜

  • public : κΈ°λ³Έκ°’, μ–΄λ””μ„œλ‚˜ μ ‘κ·Ό κ°€λŠ₯
  • protected : ν•΄λ‹Ή 클래슀 λ‚΄λΆ€ 및 μ„œλΈŒν΄λž˜μŠ€μ—μ„œ μ ‘κ·Ό κ°€λŠ₯
  • private / # (ν•΄μ‹œ 이름) : ν•΄λ‹Ή 클래슀 λ‚΄λΆ€μ—μ„œλ§Œ μ ‘κ·Ό κ°€λŠ₯

 

class Continent {
// μ ‘κ·Ό μ œμ–΄μž
    private continentName: string;
    constructor(name: string) {
        this.continentName = name;
    }
    getContinentName() {
        return this.continentName;
    }
}

class Country extends Continent {
    name: string;
    capital: string;

    constructor(continentName: string, name: string, capital: string) {
        super(continentName) // λΆ€λͺ¨λ‘œλΆ€ν„° 상속
        this.name = name;
        this.capital = capital;
    }

    getInfo() {
        // private의 κ²½μš°μ—λ§Œ μ—λŸ¬,
        // protected, publicμ—μ„œλŠ” 접근이 κ°€λŠ₯
        this.continentName;
        return `${this.name}, ${this.capital}, ${this.getContinentName()}`;
    }
}

 

ES6λΆ€ν„°λŠ” ν•΄μ‹œλ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€.

class Country extends Continent {
// ν•΄μ‹œ μ‚¬μš©
    #name: string;
    capital: string;

    constructor(continentName: string, name: string, capital: string) {
        super(continentName) // λΆ€λͺ¨λ‘œλΆ€ν„° 상속
        this.#name = name;
        this.capital = capital;
    }

    getInfo() {
        this.continentName;
        return `${this.#name}, ${this.capital}, ${this.getContinentName()}`;
    }
}

// μΈμŠ€ν„΄μŠ€ 생성
let country = new Country(
    'Asia', 'South Korea', 'Seoul'
);

country.name; // μ ‘κ·Ό λΆˆκ°€λŠ₯!

 

 

 

μΈν„°νŽ˜μ΄μŠ€λ‘œ 클래슀 ꡬ쑰 μ •μ˜

// μΈν„°νŽ˜μ΄μŠ€ μ •μ˜
interface ContinentInterface {
    getContinentName(): string
}

interface CountryInterface {
    // μ§€κΈˆμ€ name이 private(#)인 μƒνƒœμ΄λ―€λ‘œ μ œμ™Έ
    capital: string;
    getInfo(): string;
}

// 클래슀 μ •μ˜
class Continent implements ContinentInterface {
    protected continentName: string;
    constructor(name: string) {
        this.continentName = name;
    }
    getContinentName() {
        return this.continentName;
    }
}

// extends λ¨Όμ € μ‚¬μš©, implementsλŠ” κ·Έ λ‹€μŒ
class Country extends Continent implements CountryInterface {
    #name: string;
    capital: string;

    constructor(continentName: string, name: string, capital: string) {
        super(continentName) // λΆ€λͺ¨λ‘œλΆ€ν„° 상속
        this.#name = name;
        this.capital = capital;
    }

    getInfo() {
        this.continentName;
        return `${this.#name}, ${this.capital}, ${this.getContinentName()}`;
    }
}

// μΈμŠ€ν„΄μŠ€ 생성
let country = new Country(
    'Asia', 'South Korea', 'Seoul'
);

console.log(country.getInfo())

 

 

 

μΆ”μƒν΄λž˜μŠ€

abstract class AbstractCountry {
    name: string;
    capital: string;
    constructor(name: string, capital: string) {
        this.name = name;
        this.capital = capital;
    }

    setup(): void {
        console.log("setup complete")
    }
    abstract displayInfo(): void;
}

// 좔상 ν΄λž˜μŠ€λŠ” μΈμŠ€ν„΄μŠ€ μ‚¬μš©μ΄ λΆˆκ°€λŠ₯
// μƒμ†λ°›λŠ” μƒˆ 클래슀λ₯Ό μƒμ„±ν•΄μ€˜μ•Όλ¨
class MyCountry extends AbstractCountry {
    displayInfo(){
        console.log("display info called")
    }
}

const abstractCountry = new MyCountry("Germany", "Berilin")
abstractCountry.setup();
abstractCountry.displayInfo();

 

 

 

'typescript' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

TypeScript / νƒ€μž… μ„ μ–Έ 파일  (4) 2025.06.10
TypeScript / μ œλ„€λ¦­  (2) 2025.06.05
TypeScript / νƒ€μž… μΆ”λ‘ , νƒ€μž… 단언  (0) 2025.06.05
TypeScript / νƒ€μž…(2)  (0) 2025.06.04
TypeScript / νƒ€μž… (1)  (0) 2025.06.04