TypeScript Class

2taeyoonยท2023๋…„ 8์›” 23์ผ
0
post-thumbnail
post-custom-banner

๐Ÿฆฎํด๋ž˜์Šค(Class)๋ž€?

ํด๋ž˜์Šค๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ๊ธฐ๋ณธ ์š”์†Œ๋กœ, TypeScript์—์„œ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

class Goods {
    goodsName: string;
    price: number;

    constructor(goodsName: string, price: string) {
        this.goodsName = goodsName;
        this.price = price;
    }
  
     goodsPrint(): string {
        return `${this.goodsName}์˜ ๊ฐ€๊ฒฉ์€ ${this.price}์› ์ž…๋‹ˆ๋‹ค.`;
    }
}

์œ„์˜ ์ฝ”๋“œ์—์„œ constructor ๋ฉ”์„œ๋“œ๋Š” Goods ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ(ํด๋ž˜์Šค๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒ์„ฑ๋œ ๊ฐ ๊ฐ์ฒด) ์ƒ์„ฑํ•  ๋•Œ ํ˜ธ์ถœ๋˜๋ฉฐ, goodsName๊ณผ price์˜ string ๋ฐ number ์†์„ฑ์„ ์ธ์ž๋กœ ์ „๋‹ฌ๋œ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ์ดˆ๊ธฐํ™” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€๋กœ constructor ๋ฉ”์„œ๋“œ๋Š” ํด๋ž˜์Šค ๋‚ด์—์„œ ํ•œ ๋ฒˆ๋งŒ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
const goods1 = new Goods("Mouse", 3000);
const goods2 = new Goods("Keyboard", 5000);

console.log(goods1.price); // 3000
console.log(goods2.goodsName);  // Keyboard
console.log(goods1.goodsPrint()); // Mouse์˜ ๊ฐ€๊ฒฉ์€ 3000์› ์ž…๋‹ˆ๋‹ค.
console.log(goods2.goodsPrint()); // Keyboard์˜ ๊ฐ€๊ฒฉ์€ 5000์› ์ž…๋‹ˆ๋‹ค.

์œ„์˜ ์ฝ”๋“œ์—์„œ goods1๊ณผ goods2๋Š” Goods ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋กœ, ๊ฐ ์ธ์Šคํ„ด์Šค๋Š” ํด๋ž˜์Šค์˜ ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ์™€ ์†์„ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Extends๋ž€?

ํด๋ž˜์Šค ๊ฐ„ ์ƒ์†์„ ํ†ตํ•ด ๊ธฐ์กด ํด๋ž˜์Šค์˜ ํŠน์„ฑ์„ ํ™•์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

class Exhibition extends Goods {
    position: string; // Class Goods ์ƒ์† ํ›„, position ๊ฐ’ ์ถ”๊ฐ€

    constructor(goodsName: string, price: number, position: string) {
        super(goodsName, price);
        this.position = position;
    }
  	goodsInfo(): string {
    	return `${super.goodsPrint()} ํ•ด๋‹น ์ƒํ’ˆ์€${this.position}์— ์ง„์—ด๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.`;
	}
}

์œ„์˜ ์˜ˆ์‹œ์—์„œ Exhibition ํด๋ž˜์Šค๊ฐ€ Goods ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›์•„์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Super keyword

super ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ€๋ชจ ํด๋ž˜์Šค์ธ Goods์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ Exhibition ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ๋‚ด์—์„œ๋„ Goods ํด๋ž˜์Šค์˜ ์†์„ฑ์ธ goodsName๊ณผ price๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

const goodsInfo = new goodsInfo("Coffe", 1200, "A์—ด 10๋ฒˆ");
console.log(goodsInfo.goodsInfo());
// Coffee์˜ ๊ฐ€๊ฒฉ์€ 1200์› ์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ์ƒํ’ˆ์€ A์—ด 10๋ฒˆ์— ์ง„์—ด๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ ‘๊ทผ ์ œ์–ด์ž(Private, Protected, Public)

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—์„œ ์ ‘๊ทผ ์ œ์–ด์ž(public, private, protected)๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํด๋ž˜์Šค์˜ ์ ‘๊ทผ ๊ถŒํ•œ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

class Goods {
    public goodsName: string;    // public ์ ‘๊ทผ ์ œ์–ด์ž
    protected price: number;     // protected ์ ‘๊ทผ ์ œ์–ด์ž
    protected discount: number;    // protected ์ ‘๊ทผ ์ œ์–ด์ž
    private position: string;      // private ์ ‘๊ทผ ์ œ์–ด์ž

    constructor(goodsName: string, price: number, discount: number, position: string) {
        this.goodsName = goodsName;
        this.price = price;
        this.discount = discount;
        this.position = position;
    }

    public goodsPrint(): string {
        return `${this.goodsName}์˜ ๊ฐ€๊ฒฉ์€ ${this.price}์›์ด๋ฉฐ ํ•ด๋‹น ์ƒํ’ˆ์€
				${this.position}์— ์ง„์—ด๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.(ํ• ์ธ์œจ: ${this.discount}%)`;
    }
}

class DiscountedGoods extends Goods {
    constructor(goodsName: string, price: number, discount: number, position: string) {
        super(goodsName, price, discount, position);
    }
  
	// price์™€ discount๋Š” protected ์ ‘๊ทผ ์ œ์–ด์ž๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•จ!
    public discountedPrice(): string {
        return `${this.price - (this.price * this.discount / 100)}์›`;
    }
}
  • ์ ‘๊ทผ ์ œ์–ด์ž public์€ ์ ‘๊ทผ ๊ถŒํ•œ์€ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, public์€ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ์ ‘๊ทผ ์ œ์–ด์ž protected๋Š” protected๋ฅผ ์‚ฌ์šฉํ•œ ํ•ด๋‹น ํด๋ž˜์Šค์™€ ์„œ๋ธŒ ํด๋ž˜์Šค์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • ์ ‘๊ทผ ์ œ์–ด์ž private๋Š” private๋ฅผ ์‚ฌ์šฉํ•œ ํ•ด๋‹น ํด๋ž˜์Šค์—์„œ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
const goods = new Goods("Americano", 4500, 10, 'B์—ด 9๋ฒˆ');
console.log(goods.goodsName); // ์ ‘๊ทผ ๊ฐ€๋Šฅ (public)
console.log(goods.price);   // ์ ‘๊ทผ ๋ถˆ๊ฐ€ (protected)
console.log(goods.discount); // ์ ‘๊ทผ ๋ถˆ๊ฐ€ (private)
console.log(goods.goodsPrint());
// ์ถœ๋ ฅ: Americano์˜ ๊ฐ€๊ฒฉ์€ 4500์›์ด๋ฉฐ ํ•ด๋‹น ์ƒํ’ˆ์€ B์—ด 9๋ฒˆ์— ์ง„์—ด๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.(ํ• ์ธ์œจ: 10%)

const discountedGoods = new DiscountedGoods("Tea", 3000, 15, 'C์—ด 3๋ฒˆ');
console.log(discountedGoods.goodsName); // ์ ‘๊ทผ ๊ฐ€๋Šฅ (public)
console.log(discountedGoods.price);   // ์ ‘๊ทผ ๋ถˆ๊ฐ€ (protected)
console.log(discountedGoods.discount); // ์ ‘๊ทผ ๋ถˆ๊ฐ€ (private)
console.log(discountedGoods.goodsPrint());
// ์ถœ๋ ฅ: Tea์˜ ๊ฐ€๊ฒฉ์€ 3000์›์ด๋ฉฐ, ํ•ด๋‹น ์ƒํ’ˆ์€ C์—ด 3๋ฒˆ์— ์ง„์—ด๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.(ํ• ์ธ์œจ: 15%)
console.log(discountedGoods.discountedPrice()); // ์ถœ๋ ฅ: 2550 (3000 - 450)

์ถ”์ƒ ํด๋ž˜์Šค(Abstract Class)

์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์Šคํ„ด์Šค๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ํ•˜๊ณ , ํŒŒ์ƒ๋œ ํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

abstract class AbstractGoods {
    goodsName: string;
    price: number;
    discount: number; 
    position: string;

    constructor(goodsName: string, price: number, discount: number, position: string) {
        this.goodsName = goodsName;
        this.price = price;
        this.discount = discount;
        this.position = position;
    }

    abstract goodsPrint(): string;
}

abstract์œผ๋กœ ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•ด์ฃผ๊ณ  goodsPrint ๋ฉ”์†Œ๋“œ๋Š” ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋กœ ์„ ์–ธ์ด ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ๋ฐ˜๋“œ์‹œ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

class Goods extends AbstractGoods {
    constructor(goodsName: string, price: number, discount: number, position: string) {
        super(goodsName, price, discount, position);
    }

    goodsPrint(): string {
        return `${this.goodsName}์˜ ๊ฐ€๊ฒฉ์€ ${this.price}์›์ด๋ฉฐ
		ํ•ด๋‹น ์ƒํ’ˆ์€ ${this.position}์— ์ง„์—ด๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.(ํ• ์ธ์œจ: ${this.discount}%)`;
    }
}

AbstractGoos๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค Goods๋ฅผ ์„ ์–ธํ•˜๊ณ  Goods ํด๋ž˜์Šค์—์„œ goodsPrint ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ์ธ์Šคํ„ด์Šคํ™” ํ•ฉ๋‹ˆ๋‹ค.

const goods = new Goods("๋„์‹œ๋ฝ", 5000, 20, "D์—ด 7๋ฒˆ");
console.log(goods.goodsPrint());
// ๋„์‹œ๋ฝ์˜ ๊ฐ€๊ฒฉ์€ 5000์›์ด๋ฉฐ ํ•ด๋‹น ์ƒํ’ˆ์€ D์—ด 7๋ฒˆ์— ์ง„์—ด๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.(ํ• ์ธ์œจ: 20%)

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ๊ณต๋ถ€ํ•  ๋•Œ ํด๋ž˜์Šค๊ฐ€ ์–ด๋ ค์› ๋Š”๋ฐ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉฐ ํด๋ž˜์Šค๋ฅผ ๋‹ค์‹œ ๊ณต๋ถ€ํ•ด๋ณด๋‹ˆ ์—ญ์‹œ๋‚˜ ์–ด๋ ต๋‹ค..๐Ÿ˜ญ

profile
์ •๋ฆฌํ•˜๋ฉด์„œ ๊ณต๋ถ€ํ•˜๊ธฐ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€