You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/index.ts
+8-2Lines changed: 8 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -3,12 +3,16 @@
3
3
classCar{
4
4
readonlyname: string="car";
5
5
color: string;
6
+
staticwheels=4;
6
7
constructor(color: string,name: string){
7
8
this.color=color;
8
9
this.name=name;
9
10
}
10
11
start(): void{
11
12
console.log("start");
13
+
console.log(this.name);
14
+
// console.log(this.wheels); //-> error : this가 아닌 class 이름으로 접근
15
+
console.log(Car.wheels);
12
16
}
13
17
}
14
18
@@ -18,7 +22,6 @@ class Bmw extends Car {
18
22
// 참고로 'super()'는 부모(일반적인 super가 아님)의 constructor에 접근
19
23
}
20
24
showName(): void{
21
-
// console.log(super.name); //-> error 이유는 super는 부모 클래스 메서드를 호출 할 때 사용된다. 부모 속성에 접근하려면 this를 사용해야 한다. 즉, constructor에서 사용한 방식과 달리 this를 사용해야한다. 이미 extends Car를 상속받고 있기 때문이다.
22
25
console.log(this.name);
23
26
// - private: 부모 name이 private인 경우 error가 나온다. 추가적으로 '#name'은 private로 인식한다.
24
27
// - protected: 부모 name이 protected인 경우 정상 동작한다. 그렇다면 public과 차이는 무엇인가.
@@ -29,9 +32,12 @@ class Bmw extends Car {
29
32
/*
30
33
* 부모 constructor 내부에서 작업을 해야한다.
31
34
*/
35
+
// - static: static은 정적 맴버 변수를 만들어 줄 수 있다. static에 접근을 하기 위해서는 this가 아닌 class 이름으로 접근을 해야 한다.
32
36
}
33
37
}
34
38
35
39
constz4=newBmw("black","test");
36
40
console.log(z4.name);
37
-
// z4.name = "test"; // error -> 현재 name은 readonly이기 때문에 인스턴스 생성자에서 변경을 해야한다.
41
+
// z4.name = "test";
42
+
// console.log(z4.wheels); //-> error : this가 아닌 class 이름으로 접근
0 commit comments