TS
[TypeScript] Polymorphism(제네릭, 인터페이스,클래스)
코딩하는둥이
2023. 2. 28. 10:38
Polymorphism (다형성)
: 다른 모양의 코드를 가질 수 있게 함
제네릭
: placeholder 타입을 쓸 수 있돌고 해줌
interface SStorage<T>{
[key:string]:T //property
}
class LocalStorage<T>{
private storage :SStorage<T> = {}
set(key:string,value:T){
this.storage[key] = value;
}
remove(key:string){
delete this.storage[key]
}
get(key:string):T{
return this.storage[key]
}
clear(){
this.storage = {}
}
}
const stringStorage = new LocalStorage<string>()
stringStorage.get("key")
stringStorage.set("hello","how are you")
const booleansStorage = new LocalStorage<boolean>()
booleansStorage.get("xxx")
booleansStorage.set("xxx",true)