typescript class add delete update showall count
2022. 7. 3. 02:21ㆍ카테고리 없음
/*
Typescript Classes 를 사용하여, Dict (dictionary) class 를 빌드하세요. Dict은 아래와 같은 methods 를 갖고있어야 합니다.
add: 단어를 추가함.
get: 단어의 정의를 리턴함.
delete: 단어를 삭제함.
update: 단어를 업데이트 함.
showAll: dictionary 의 단어를 모두 프린트함.
count: dict 단어들의 총 count 를 리턴함.
*/
type Words = {
[key:string]:string // property나 key의 타입을 정의
}
class Dict {
protected words:Words
constructor(){
this.words = {}
}
add(word:Word){ // 단어 추가 메소드 정의
if(this.words[word.term]=== undefined){
this.words[word.term] = word.def;
}
}
get(term:string){
return this.words[term]
}
delete(term:string){
delete this.words[term]
}
update(word:Word){
if(this.words[word.term]!== undefined){
this.words[word.term] = word.def
}
}
showAll(){
console.log(this.words);
return this.words;
}
count(){
console.log(Object.keys(this.words).length);
return Object.keys(this.words).length;
}
}
class Word {
constructor(
public term:string,
public def:string
){}
}
const kimchi = new Word("kimchi","매우 맛있다"); // term, def
const kimchi2 = new Word("kimchi","지금도 먹고싶당"); // term, def
const noodle = new Word("noodle","컵라면이 조아"); // term, def
const bacon = new Word("베이컨","스펠링은 몰라도 조아"); // term, def
const dict = new Dict();
dict.add(kimchi);
dict.add(noodle);
dict.add(bacon);
dict.get('kimchi');
// dict.delete('kimchi');
dict.update(kimchi2);
dict.showAll();
dict.count();