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+
1916class 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