class Calculator {
constructor(
private firstName : string,
private lastName : string,
private shares : number,
private company : string
) {}
public calculateNetWorth() {
return this.shares * getSharePrice(this.company);
}
public getName() {
return `${this.firstName} ${this.lastName}`
}
}
const nolec = new Calculator("nolec", "Noh", 100000, "삼성전자");
console.log(nolec.getName());
위 내용을 생각하면서 JS의 배열을 추상화해보자
class BetterArray {
private items: string[];
constructor(){
this.items = [];
}
public getItems(){
return [...this.items];
}
public addItem(item: string){
this.items.push(item);
}
public removeItem(itemToDelete: string){
this.items = this.items.filter((item) => item !== itemToDelete);
}
public modifyItem(itemToChange: string, newValue: string){
const index = this.items.indexOf(itemToChange);
if(index !== -1){
this.items[index] = newValue
}
}
}
class Person {
public sayHello(){
return "🖐🖐";
}
public sayBye(){
return "bye~";
}
}
class Korean extends Person {}
class Italian extends Person {}
class Korean extends Person {
public sayHello(){
return "안녕하세요";
}
}
덕분에 클래스의 핵심은 그대로 있으며 구현방식의 모양과 모습만 달라지게 되는 것
위 글은 노마드코더의 객체지향 영상을 참고하였습니다.