c++, java랑 동일.
implements를 사용해 클래스가 특정 인터페이스를 구현하게 할 수 있음public인 필드만 인터페이스 조건을 만족함타입스크립트의 인터페이스는 클래스의 설계도 역할을 할 수 있다.
클래스가 어떤 필드와 메서드를 가져야 하는지 명확히 정의할 수 있다.
interface CharacterInterface {
name: string;
moveSpeed: number;
move(): void;
}
CharacterInterface는 다음 3가지를 요구한다:
name: 문자열moveSpeed: 숫자move(): 반환값이 없는 함수인터페이스를 클래스에 적용하려면 implements 키워드를 사용한다.
class Character implements CharacterInterface {
constructor(
public name: string,
public moveSpeed: number,
private extra: string
) {}
move(): void {
console.log(`${this.moveSpeed} 속도로 이동!`);
}
}
implements를 사용하면, 클래스는 해당 인터페이스를 완전히 구현해야 한다.public으로 선언된 필드는 인터페이스 요구사항을 만족시킨다.extra 필드는 private이기 때문에 인터페이스에 없어도 무관하다.인터페이스 기반 설계를 활용하면 코드의 일관성과 가독성,
그리고 유지보수성이 크게 향상된다.