1
- //์ ๊ทผ ์ ํ์(Access modifier) - public, private, protected / es6์์๋ ์ ๊ทผ ์ ํ์๋ฅผ ์ง์ํ์ง ์์๋ค ํ์ง๋ง ts์์๋ ์ ๊ณตํด์ค๋ค.
1
+ // Generic : ํด๋์ค๋ ํ์
์ ์ฌ์ฌ์ฉํ๊ธฐ ์ํ ๋ฌธ๋ฒ
2
+ // ์ฌ๋ฌ๊ฐ์ ํ์
์ ์ฌ์ฉํ๋ ค๋ฉด '์ ๋์จ ํ์
' ๋๋ '์ค๋ฒ๋ก๋'๋ฅผ ์ฌ์ฉํ๋ค.
2
3
3
- abstract class Car {
4
- color : string ;
5
- constructor ( color : string ) {
6
- this . color = color ;
7
- }
8
- start ( ) : void {
9
- console . log ( "start" ) ;
10
- }
11
- abstract doSomething ( ) : string ; //์๋ฌด ์์
์ ํ์ง ์์ ์ถ์ ํจ์ ์์๋ฐ์ ํด๋์ค์ ํด๋น ํจ์๊ฐ ์๋ค๋ฉด error ๋ฐ์
4
+ function getSize ( arr : number [ ] | string [ ] | boolean [ ] | object [ ] ) : number {
5
+ return arr . length ;
12
6
}
13
7
14
- // const Car = new Car("red"); //-> error : ์ถ์ํด๋์ค๋ new ์ธ์คํด์ค๋ก ์์ฑํ ์ ์๋ค. ์ถ์ํด๋์ค๋ ์ฒญ์ฌ์ง๊ณผ ๊ฐ๋ค๊ณ ์๊ฐํ๋ฉฐ ๋๋ค. ๋ฐ๋ผ์ ์ค๋ก์ง ์์์ ํตํด์๋ง ๊ฐ๋ฅํ๋ค.
8
+ // number Type
9
+ const arr1 = [ 1 , 2 , 3 ] ;
10
+ getSize ( arr1 ) ; //3
15
11
16
- class Bmw extends Car {
17
- constructor ( color : string ) {
18
- super ( color ) ;
19
- // ์ฐธ๊ณ ๋ก 'super()'๋ ๋ถ๋ชจ(์ผ๋ฐ์ ์ธ super๊ฐ ์๋)์ constructor์ ์ ๊ทผ
20
- }
21
- // abstract doSomething()๊ฐ ์๋ค๋ฉด class๋ error, ์ฆ, ์ถ์ ํด๋์ค๋ ์์์ ๋ฐ์ ์ชฝ์์ ํด๋น ํจ์๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
22
- doSomething ( ) : string {
23
- // alert("do!"); //-> error : alert๋ browser์์๋ง ์ฌ์ฉ์ด ๊ฐ๋ฅ
24
- console . log ( "do!" ) ;
25
- return 'Do!' ;
26
- }
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) ๋๋ ํจ์, ๋ณ์๋ฅผ ๋ง๋ค์ด ์ค๋ค.
38
- }
12
+ // string Type
13
+ const arr2 = [ "a" , "b" , "c" ] ;
14
+ getSize ( arr2 ) ; //3
15
+
16
+ //ํ์ง๋ง ์ด๋ณด๋ค ๋ ์ฌ๋ฌ๊ฐ์ ํ์
์ ์ง์ ํ๊ฑฐ๋ ์ฌ์ฉ ํด์ผํ๋ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น? ์ ๋์จ ํ์
์ผ๋ก ๋ชจ๋ ๋ค ์ง์ ํ๋ ๊ฒ์ ๋๋ฌด ๊ฐ๋
์ฑ์ด ๋จ์ด์ง๋ค.
17
+
18
+ // boolean Type
19
+ const arr3 = [ false , true , true ] ;
20
+ getSize ( arr3 ) ;
39
21
40
- const z4 = new Bmw ( "black" ) ;
41
- console . log ( z4 . doSomething ( ) ) ; //undefined๊ฐ ์ถ๋ ฅ๋๋ ์ด์ : doSomething์ ๋ฐํ ๊ฐ์ด void์ด๊ธฐ ๋๋ฌธ์ด๋ค.
22
+ // object Type
23
+ const arr4 = [ { } , { } , { name : 'Time' } ] ;
24
+ getSize ( arr4 ) ;
0 commit comments