Skip to content

Commit 87f82fd

Browse files
Merge pull request #68 from yahma25/Translation-to-ko-playgroun-Generic-Classes
Translate 1 file to ko - Generic Classes
2 parents 9b1713b + a6fdaa2 commit 87f82fd

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//// { order: 3 }
2+
3+
// 먼저 이 방식으로 이해하는 게 훨씬 쉬우므로
4+
// 예시는 대부분 TypeScript로 작성했습니다.
5+
// 마지막에는 JSDoc을 사용하여 동일한 클래스를 만드는 방법을 다룰 겁니다.
6+
7+
// 제네릭 클래스는 특정 타입이 다른 타입에 따라 동작한다는 것을 보여주는 하나의 방식입니다.
8+
// 예를 들어, 여기에는 한 종류만 있지만
9+
// 여러 가지 물건을 담을 수 있는 하나의 서랍이 있습니다:
10+
11+
class Drawer<ClothingType> {
12+
contents: ClothingType[] = [];
13+
14+
add(object: ClothingType) {
15+
this.contents.push(object);
16+
}
17+
18+
remove() {
19+
return this.contents.pop();
20+
}
21+
}
22+
23+
// Drawer를 사용하기 위해서
24+
// 또 다른 타입이 필요합니다:
25+
26+
interface Sock {
27+
color: string;
28+
}
29+
30+
interface TShirt {
31+
size: "s" | "m" | "l";
32+
}
33+
34+
// 새로운 Drawer를 만들 때 Sock 타입을 전달하여
35+
// 양말 전용의 새로운 Drawer를 만들 수 있습니다:
36+
const sockDrawer = new Drawer<Sock>();
37+
38+
// 이제 양말을 서랍에 추가하거나 삭제할 수 있습니다:
39+
sockDrawer.add({ color: "white" });
40+
const mySock = sockDrawer.remove();
41+
42+
// 티셔츠를 위한 서랍도 만들 수 있습니다:
43+
const tshirtDrawer = new Drawer<TShirt>();
44+
tshirtDrawer.add({ size: "m" });
45+
46+
// 여러분이 조금 별나신 편이라면,
47+
// 유니언을 사용하여 양말과 티셔츠가 섞인 서랍을 만들 수도 있습니다
48+
49+
const mixedDrawer = new Drawer<Sock | TShirt>();
50+
51+
// 추가 TypeScript 구문 없이 Drawer와 같은 클래스를 만드는 것은
52+
// JSDoc에서 템플릿 태그 사용을 요구합니다.
53+
// 이 예시에서 템플릿 변수를 정의하고,
54+
// 클래스에 프로퍼티를 제공할 것입니다:
55+
56+
// playground에서 작업하기 위해서,
57+
// JavaScript 파일로 설정을 변경하고
58+
// 위에 있는 TypeScript 코드를 제거해야 합니다
59+
60+
/**
61+
* @template {{}} ClothingType
62+
*/
63+
class Dresser {
64+
constructor() {
65+
/** @type {ClothingType[]} */
66+
this.contents = [];
67+
}
68+
69+
/** @param {ClothingType} object */
70+
add(object) {
71+
this.contents.push(object);
72+
}
73+
74+
/** @return {ClothingType} */
75+
remove() {
76+
return this.contents.pop();
77+
}
78+
}
79+
80+
// 그러고 나서 JSDoc을 통해 새로운 타입을 만듭니다:
81+
82+
/**
83+
* @typedef {Object} Coat 의류 아이템
84+
* @property {string} color 코트 색상
85+
*/
86+
87+
// Dresser 클래스의 새로운 인스턴스를 생성할 때
88+
// 코트를 다루는 Dresser로
89+
// 변수를 할당하기 위해 @type을 사용합니다.
90+
91+
/** @type {Dresser<Coat>} */
92+
const coatDresser = new Dresser();
93+
94+
coatDresser.add({ color: "green" });
95+
const coat = coatDresser.remove();

0 commit comments

Comments
 (0)