Skip to content

Commit 786992c

Browse files
committed
๐Ÿšฉ: abstract์˜ ๋Œ€ํ•œ ๊ฐœ๋…๊ณผ ํ•จ์ˆ˜๊ฐ€ void์ผ ๊ฒฝ์šฐ ์ถœ๋ ฅ๋˜๋Š” undefined์˜ ๋Œ€ํ•œ ๊ฐœ๋…
1 parent 7fe944d commit 786992c

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

โ€Žsrc/index.tsโ€Ž

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,41 @@
11
//์ ‘๊ทผ ์ œํ•œ์ž(Access modifier) - public, private, protected / es6์—์„œ๋Š” ์ ‘๊ทผ ์ œํ•œ์ž๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์•˜๋‹ค ํ•˜์ง€๋งŒ ts์—์„œ๋Š” ์ œ๊ณตํ•ด์ค€๋‹ค.
22

3-
class Car {
4-
readonly name: string = "car";
3+
abstract class Car {
54
color: string;
6-
static wheels = 4;
7-
constructor(color: string, name: string) {
5+
constructor(color: string) {
86
this.color = color;
9-
this.name = name;
107
}
118
start(): void {
129
console.log("start");
13-
console.log(this.name);
14-
// console.log(this.wheels); //-> error : this๊ฐ€ ์•„๋‹Œ class ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผ
15-
console.log(Car.wheels);
1610
}
11+
abstract doSomething(): string; //์•„๋ฌด ์ž‘์—…์„ ํ•˜์ง€ ์•Š์€ ์ถ”์ƒ ํ•จ์ˆ˜ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค์— ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ์—†๋‹ค๋ฉด error ๋ฐœ์ƒ
1712
}
1813

14+
// const Car = new Car("red"); //-> error : ์ถ”์ƒํด๋ž˜์Šค๋Š” new ์ธ์Šคํ„ด์Šค๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋‹ค. ์ถ”์ƒํด๋ž˜์Šค๋Š” ์ฒญ์‚ฌ์ง„๊ณผ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉฐ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์˜ค๋กœ์ง€ ์ƒ์†์„ ํ†ตํ•ด์„œ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.
15+
1916
class Bmw extends Car {
20-
constructor(color: string, name: string) {
21-
super(color, name);
17+
constructor(color: string) {
18+
super(color);
2219
// ์ฐธ๊ณ ๋กœ 'super()'๋Š” ๋ถ€๋ชจ(์ผ๋ฐ˜์ ์ธ super๊ฐ€ ์•„๋‹˜)์˜ constructor์— ์ ‘๊ทผ
2320
}
24-
showName(): void {
25-
console.log(this.name);
26-
// - private: ๋ถ€๋ชจ name์ด private์ธ ๊ฒฝ์šฐ error๊ฐ€ ๋‚˜์˜จ๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ '#name'์€ private๋กœ ์ธ์‹ํ•œ๋‹ค.
27-
// - protected: ๋ถ€๋ชจ name์ด protected์ธ ๊ฒฝ์šฐ ์ •์ƒ ๋™์ž‘ํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด public๊ณผ ์ฐจ์ด๋Š” ๋ฌด์—‡์ธ๊ฐ€.
28-
/*
29-
* new๋ฅผ ํ†ตํ•ด์„œ ์ธ์Šคํ„ด์Šค ํ–ˆ์„ ๊ฒฝ์šฐ ์ ‘๊ทผ์„ ์ œํ•œํ•˜๊ฒŒ ๋œ๋‹ค. ์ฆ‰, ์ž์‹ ํด๋ž˜์Šค์—์„œ๋Š” ๋ถ€๋ชจ ์†์„ฑ์— ์ ‘๊ทผ์„ ํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ new ์ธ์Šคํ„ด์Šค์ธ ๊ฒฝ์šฐ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€ํ•˜๋‹ค.
30-
*/
31-
// - readonly: new ์ธ์Šคํ„ด์Šค๋ฅผ ํ†ตํ•ด์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ๋ณ€๊ฒฝ์„ ํ•  ์ˆ˜ ์žˆ์„๊นŒ?
32-
/*
33-
* ๋ถ€๋ชจ constructor ๋‚ด๋ถ€์—์„œ ์ž‘์—…์„ ํ•ด์•ผํ•œ๋‹ค.
34-
*/
35-
// - static: static์€ ์ •์  ๋งด๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ค„ ์ˆ˜ ์žˆ๋‹ค. static์— ์ ‘๊ทผ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” this๊ฐ€ ์•„๋‹Œ class ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผ์„ ํ•ด์•ผ ํ•œ๋‹ค.
21+
// abstract doSomething()๊ฐ€ ์—†๋‹ค๋ฉด class๋Š” error, ์ฆ‰, ์ถ”์ƒ ํด๋ž˜์Šค๋Š” ์ƒ์†์„ ๋ฐ›์€ ์ชฝ์—์„œ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
22+
doSomething(): string {
23+
// alert("do!"); //-> error : alert๋Š” browser์—์„œ๋งŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅ
24+
console.log("do!");
25+
return 'Do!';
3626
}
27+
// - private: ๋ถ€๋ชจ name์ด private์ธ ๊ฒฝ์šฐ error๊ฐ€ ๋‚˜์˜จ๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ '#name'์€ private๋กœ ์ธ์‹ํ•œ๋‹ค.
28+
// - protected: ๋ถ€๋ชจ name์ด protected์ธ ๊ฒฝ์šฐ ์ •์ƒ ๋™์ž‘ํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด public๊ณผ ์ฐจ์ด๋Š” ๋ฌด์—‡์ธ๊ฐ€.
29+
/*
30+
* new๋ฅผ ํ†ตํ•ด์„œ ์ธ์Šคํ„ด์Šค ํ–ˆ์„ ๊ฒฝ์šฐ ์ ‘๊ทผ์„ ์ œํ•œํ•˜๊ฒŒ ๋œ๋‹ค. ์ฆ‰, ์ž์‹ ํด๋ž˜์Šค์—์„œ๋Š” ๋ถ€๋ชจ ์†์„ฑ์— ์ ‘๊ทผ์„ ํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ new ์ธ์Šคํ„ด์Šค์ธ ๊ฒฝ์šฐ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€ํ•˜๋‹ค.
31+
*/
32+
// - readonly: new ์ธ์Šคํ„ด์Šค๋ฅผ ํ†ตํ•ด์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ๋ณ€๊ฒฝ์„ ํ•  ์ˆ˜ ์žˆ์„๊นŒ?
33+
/*
34+
* ๋ถ€๋ชจ constructor ๋‚ด๋ถ€์—์„œ ์ž‘์—…์„ ํ•ด์•ผํ•œ๋‹ค.
35+
*/
36+
// - static: static์€ ์ •์  ๋งด๋ฒ„ ๋ณ€์ˆ˜(property)๋ฅผ ๋งŒ๋“ค์–ด ์ค„ ์ˆ˜ ์žˆ๋‹ค. static์— ์ ‘๊ทผ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” this๊ฐ€ ์•„๋‹Œ class ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผ์„ ํ•ด์•ผ ํ•œ๋‹ค.
37+
// - abstract: abstract์€ ์ถ”์ƒ ํด๋ž˜์Šค(class) ๋˜๋Š” ํ•จ์ˆ˜, ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค.
3738
}
3839

39-
const z4 = new Bmw("black", "test");
40-
console.log(z4.name);
41-
// z4.name = "test";
42-
// console.log(z4.wheels); //-> error : this๊ฐ€ ์•„๋‹Œ class ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผ
43-
console.log(Car.wheels);
40+
const z4 = new Bmw("black");
41+
console.log(z4.doSomething()); //undefined๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ์ด์œ  : doSomething์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด void์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

0 commit comments

Comments
ย (0)