GeezerVison is showing github.com — docs: interfase 상속 추가 · dev-language/TypeScript_prac@8b2e5e7
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +42Lines changed: 42 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +{ | | 2 | + var myDog = { name: "넷째", makeSound: "왈왈" }; | | 3 | + console.log("myDog --->", myDog); | | 4 | + var School = { | | 5 | + name: "선중", | | 6 | + age: 28, | | 7 | + phone: 820100100 | | 8 | + }; | | 9 | + console.log("School --->", School); | | 10 | + var product = { | | 11 | + brand: "Samsung", | | 12 | + serialNumber: 1360, | | 13 | + model: ["TV", "phone"] | | 14 | + }; | | 15 | + var 장바구니 = [ | | 16 | + { product: "청소기", price: 7000 }, | | 17 | + { product: "삼다수", price: 800 }, | | 18 | + ]; | | 19 | + var 지갑 = { product: "마우스", price: 20000, card: true }; | | 20 | + var Calc = /** @class */ (function () { | | 21 | + function Calc() { | | 22 | + } | | 23 | + Calc.prototype.plus = function (a, b) { | | 24 | + return a + b; | | 25 | + }; | | 26 | + Calc.prototype.minus = function (a, b) { | | 27 | + return a - b; | | 28 | + }; | | 29 | + return Calc; | | 30 | + }()); | | 31 | + var newCalc = { | | 32 | + plus: function (a, b) { | | 33 | + return a + b; | | 34 | + }, | | 35 | + minus: function (a, b) { | | 36 | + return a - b; | | 37 | + } | | 38 | + }; | | 39 | + console.log("obj--->", newCalc.plus(1, 5)); | | 40 | + var calc = new Calc().plus(2, 3); | | 41 | + console.log("calc --->", calc); | | 42 | +} |
Collapse file Copy file name to clipboard +112Lines changed: 112 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +{ | | 2 | + /** | | 3 | + * ✨ 상속 ✨ * | | 4 | +* extends는 일반 클래스와 abstract 클래스 상속에 사용하고, | | 5 | +* implement는 interface 상속에 사용된다. | | 6 | +
| | 7 | +* class가 class를 상속받을 땐 extends를 사용하고, | | 8 | +* interface가 interface를 상속 받을 땐 extends를 사용한다. | | 9 | +
| | 10 | +* class가 interface를 사용할 땐 implements를 써야하고, | | 11 | +* interface가 class를 사용할 땐 implements를 쓸수 없다. | | 12 | +
| | 13 | +* extends는 클래스 한 개만 상속 받을 수 있다. | | 14 | +* extends 자식 클래스는 부모 클래스의 기능을 사용한다. | | 15 | +
| | 16 | +* implements는 여러개 사용 가능하다. | | 17 | +* implements는 설계 목적으로 구현 가능하다. | | 18 | +* implements한 클래스는 implements의 내용을 전부 사용해야 한다. | | 19 | +*/ | | 20 | + | | 21 | + /* ======= Type ======= */ | | 22 | + /** | | 23 | + * ✨ Type: 중복 선언 불가능 (업격 모드) | | 24 | + * type User = {}; | | 25 | + * type User = {}; //불가능 | | 26 | + * - & 기호로 상속할때 중복 속성이 발생시 에러를 잡아주지 않는다. | | 27 | + */ | | 28 | + | | 29 | + type Animal = { name: string }; | | 30 | + type Dog = { makeSound: string } & Animal; | | 31 | + | | 32 | + const myDog: Dog = { name: "넷째", makeSound: "왈왈" }; | | 33 | + console.log("myDog --->", myDog); | | 34 | + | | 35 | + /* ======= Interfase ======= */ | | 36 | + /** | | 37 | + * ✨ Interface: 중복 선언 가능 (중복 된 객체 병합) | | 38 | + * - interface User {}; | | 39 | + * - interface User {} //가능 | | 40 | + * 장점 : 외부 라이브러리같은 경우 interface를 많이 쓴다. (속성 추가 및 수정 가능) | | 41 | + * - 다른 사람이 많이 이용할 것 같으면 object를 Interface로 지정해도 좋음 | | 42 | + * - extends시 중복 객체값이 있으면 에러가 발생한다. | | 43 | + */ | | 44 | + | | 45 | + interface Student { | | 46 | + name: string; | | 47 | + age: number; | | 48 | + } | | 49 | + | | 50 | + interface Teacher extends Student { | | 51 | + phone: number; | | 52 | + } | | 53 | + | | 54 | + const School: Teacher = { | | 55 | + name: "선중", | | 56 | + age: 28, | | 57 | + phone: 820100100, | | 58 | + }; | | 59 | + console.log("School --->", School); | | 60 | + | | 61 | + interface Products { | | 62 | + brand: string; | | 63 | + serialNumber: number; | | 64 | + model: string[]; | | 65 | + } | | 66 | + let product: Products = { | | 67 | + brand: "Samsung", | | 68 | + serialNumber: 1360, | | 69 | + model: ["TV", "phone"], | | 70 | + }; | | 71 | + | | 72 | + interface Cart { | | 73 | + product: string; | | 74 | + price: number; | | 75 | + } | | 76 | + let 장바구니: Cart[] = [ | | 77 | + { product: "청소기", price: 7000 }, | | 78 | + { product: "삼다수", price: 800 }, | | 79 | + ]; | | 80 | + | | 81 | + interface Card extends Cart { | | 82 | + card: boolean; | | 83 | + } | | 84 | + let 지갑: Card = { product: "마우스", price: 20000, card: true }; | | 85 | + | | 86 | + interface Param { | | 87 | + plus: (a: number, b: number) => number; | | 88 | + minus: (a: number, b: number) => number; | | 89 | + } | | 90 | + class Calc implements Param { | | 91 | + plus(a: number, b: number) { | | 92 | + return a + b; | | 93 | + } | | 94 | + minus(a: number, b: number) { | | 95 | + return a - b; | | 96 | + } | | 97 | + } | | 98 | + | | 99 | + let newCalc: Param = { | | 100 | + plus(a, b) { | | 101 | + return a + b; | | 102 | + }, | | 103 | + minus(a, b) { | | 104 | + return a - b; | | 105 | + }, | | 106 | + }; | | 107 | + | | 108 | + console.log("obj--->", newCalc.plus(1, 5)); | | 109 | + | | 110 | + let calc = new Calc().plus(2, 3); | | 111 | + console.log("calc --->", calc); | | 112 | +} |
|