▶ Practice_Dictionary
type Words = {
[key:string]: string
// 할당받는 것의 type(이름)도 string, 할당받는 값도 string이라는 syntax
// property의 이름은 모르지만 type만 알 경우, 유용하게 사용되어짐! Important!
}
class Word {
constructor(
public term:string,
public def:string
){}
//def add Method
add(addWord:string){
this.def=this.def.concat('',addWord);
}
//def update Method
update(newDef:string){
this.def=newDef;
}
//def print Method
print(){
console.log(this.def)
}
}
class Dict {
private words:Words
constructor(){
this.words= {}
}
//추가 Method
add(word:Word){
// 추가하려는 word가 존재하지 않을 경우!
if(this.words[word.term]===undefined){
this.words[word.term]=word.def;
}
}
//word update Method
update(word:Word) {
if (this.words[word.term] !== undefined) {
this.words[word.term] = word.def;
}
}
//word 삭제 Method
del(term:string){
return delete this.words[term]
}
//word_def 출력 Method
def(term:string){
return this.words[term]
}
}
const kimchi = new Word("kimchi","Korean T Food");
const dict = new Dict()
dict.add(kimchi)
dict.def("kimchi")
'Web_Study > TypeScript' 카테고리의 다른 글
TypeScript_9:Interfaces_2 (0) | 2022.06.14 |
---|---|
TypeScript_9:Interfaces (0) | 2022.06.10 |
TypeScript_7:Class (0) | 2022.06.07 |
TypeScript_6:Generic_2 (0) | 2022.06.02 |
TypeScript_5:Polymorphism & Generic (0) | 2022.05.31 |