조건을 만족하는 Pokemon 클래스 완성
3개의 필드
- name 필드는 String 타입이며 Public이다.
- skill 필드는 String 타입이며 Public이다.
- type 필드는 String 타입이며 읽기 전용 필드이다.
2개의 메서드
- getName 메서드는 name 필드의 값을 반환한다.
- setSkill 메서드는 String 타입의 매개변수를 받아 skill 필드의 값을 업데이트 한다.
// 기본 버전 (this 할당 방식)
class Pokemon {
name: string;
skill: string;
readonly type: string;
constructor(name: string, skill: string, type: string) {
this.name = name;
this.skill = skill;
this.type = type;
}
getName(): string {
return this.name;
}
setSkill(skill: string): void {
this.skill = skill;
}
}
// 축약 버전 (생성자 매개변수 속성 사용)
class Pokemon {
constructor(
name: string,
skill: string,
readonly type: string
) {}
getName(): string {
return this.name;
}
setSkill(skill: string): void {
this.skill = skill;
}
}
'TIL' 카테고리의 다른 글
| [TS] 제네릭 (0) | 2025.11.04 |
|---|---|
| [CS50] 컴퓨팅 사고 - 2진법 (0) | 2025.11.03 |
| [TS] 인터페이스로 구현하는 클래스 (implements) (0) | 2025.11.03 |
| [TS] 접근 제어자 (0) | 2025.11.01 |
| [TS] 클래스 (0) | 2025.11.01 |
