1- //intersection types
1+ //์ ๊ทผ ์ ํ์(Access modifier) - public, private, protected / es6์์๋ ์ ๊ทผ ์ ํ์๋ฅผ ์ง์ํ์ง ์์๋ค ํ์ง๋ง ts์์๋ ์ ๊ณตํด์ค๋ค.
22
3- import { InterCar , InterToy } from "./utils/data.interface" ;
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 ๋ฐ์
12+ }
413
5- const toyCar : InterToy & InterCar = {
6- name : 'ํ์' ,
7- start ( ) { } ,
8- color : 'blue' ,
9- price : 1000 ,
10- }
14+ // const Car = new Car("red"); //-> error : ์ถ์ํด๋์ค๋ new ์ธ์คํด์ค๋ก ์์ฑํ ์ ์๋ค. ์ถ์ํด๋์ค๋ ์ฒญ์ฌ์ง๊ณผ ๊ฐ๋ค๊ณ ์๊ฐํ๋ฉฐ ๋๋ค. ๋ฐ๋ผ์ ์ค๋ก์ง ์์์ ํตํด์๋ง ๊ฐ๋ฅํ๋ค.
15+
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+ }
39+
40+ const z4 = new Bmw ( "black" ) ;
41+ console . log ( z4 . doSomething ( ) ) ; //undefined๊ฐ ์ถ๋ ฅ๋๋ ์ด์ : doSomething์ ๋ฐํ ๊ฐ์ด void์ด๊ธฐ ๋๋ฌธ์ด๋ค.
0 commit comments