상추의 IT저장소
TS) 추상 클래스 (Abstract class) 본문
추상화(Abstraction)
- 추상화는 객체지향 프로그래밍의 핵심아이디어 중 하나이다. 복잡성을 최소화하고 하위 수준의 세부사항을 미리 구현할 필요가 없이 상위 수준에 집중하고 나중에 세부 사항을 구현할 수 있게 해준다.
- OOP에서는 두가지 유형의 클래스가 존재한다. 추상클래스와 구체 클래스이다.
- 구체 클래스는 new 키워드를 통해 객체를 생성할 수 있다.
- 반면에 추상 클래스는 구체 클래스가 가져야 하는 속성과 함수를 설정하는 클래스이다.
- 추상클래스는 클래스 앞에 'abstract' 키워드를 붙여 선언한다.
- 하지만 추상 클래스는 구체 클래스의 설계서이므로 인스턴스를 생성할 수 없다.
TypeScript의 추상클래스
- 타입스크립트에서 추상 클래스를 사용하는 가장 일반적인 용도는 구체클래스에서 공통 동작을 찾는 것이다.
예제))
abstract class Linux {
protected abstract _description?: string;
constructor(protected _name: string, protected _version : number) {
}
abstract operation() : void; // 운영체제가 컴퓨터를 켤때
turnOff() {
console.log("모든 프로그램 종료 후 컴퓨터 끄기")
}
}
class RedHat extends Linux {
protected _description?: string;
constructor(_name : string, _version: number, _description :string) {
super(_name, _version);
if(_description) this._description = _description
}
operation(): void {
console.log("컴퓨터 부팅!")
}
}
class Demian extends Linux {
protected _description?: string;
constructor(_name : string, _version: number, _description :string) {
super(_name, _version);
if(_description) this._description = _description
}
operation(): void {
console.log("컴퓨터 부팅!")
}
}
- 추상클래스 Linux를 상속받아 구체 클래스인 RedHat과 Demian 클래스를 정의함.
참조 : https://developer-talk.tistory.com/368
'Javascript > TypeScript' 카테고리의 다른 글
| TS) 타입 단언(Type Asssertion) (0) | 2023.01.27 |
|---|---|
| TS) generic (0) | 2023.01.23 |
| TS) 타입 지정 (0) | 2023.01.23 |
| TS)Class (0) | 2023.01.17 |
| TS) TS설치 & tsconfig.json 만들기 (0) | 2023.01.17 |