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-6Lines changed: 8 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,21 @@
1
1
//접근 제한자(Access modifier) - public, private, protected / es6에서는 접근 제한자를 지원하지 않았다 하지만 ts에서는 제공해준다.
2
2
3
3
classCar{
4
-
name: string="car";
4
+
readonlyname: string="car";
5
5
color: string;
6
-
constructor(color: string){
6
+
constructor(color: string,name: string){
7
7
this.color=color;
8
+
this.name=name;
8
9
}
9
10
start(): void{
10
11
console.log("start");
11
12
}
12
13
}
13
14
14
15
classBmwextendsCar{
15
-
constructor(color: string){
16
-
super(color);
16
+
constructor(color: string,name: string){
17
+
super(color,name);
18
+
// 참고로 'super()'는 부모(일반적인 super가 아님)의 constructor에 접근
17
19
}
18
20
showName(): void{
19
21
// console.log(super.name); //-> error 이유는 super는 부모 클래스 메서드를 호출 할 때 사용된다. 부모 속성에 접근하려면 this를 사용해야 한다. 즉, constructor에서 사용한 방식과 달리 this를 사용해야한다. 이미 extends Car를 상속받고 있기 때문이다.
@@ -30,6 +32,6 @@ class Bmw extends Car {
30
32
}
31
33
}
32
34
33
-
constz4=newBmw("black");
35
+
constz4=newBmw("black","test");
34
36
console.log(z4.name);
35
-
z4.name="test";
37
+
// z4.name = "test"; // error -> 현재 name은 readonly이기 때문에 인스턴스 생성자에서 변경을 해야한다.
0 commit comments