728x90
반응형
클래스 (Class)
- 사용예시 :
class Player {
constructor (
private firstName : string,
private lastName : string,
public nickname : string,
) {}
}
const mindu = new Player("mindy", "k", "minduu");
// mindu.firstname;
console.log(mindu.firstname); //undefined
console.log(mindu.nickname); //minduu
추상 클래스 (Abstract Class)
다른 클래스가 상속 받을 수 있는 클래스로 따라야 할 청사진(설계도)를 제시해주는 역할
인스턴스를 직접 생성할 수 없고 오직 다른곳에서 상속받을 수 있음
- 사용예시 :
abstract class User {
constructor (
private firstName : string,
private lastName : string,
public nickname : string,
) {}
}
class Player extends User {
}
const mindu = new Player("mindy", "k", "minduu");
// mindu.firstname;
console.log(mindu.firstname);
console.log(mindu.nickname);
abstract class User {
constructor (
protected firstName : string,
protected lastName : string
) {}
abstract sayHi(name:string) : string
abstract fullName(name:string) : string
}
class Player extends User {
fullName() {
return `${this.firstName} ${this.lastName}`
}
sayHi(name:string) {
return `Hello ${name}, I AM ${this.firstName}`
}
}
추상 메소드 (Abstract Method)
추상 클래스를 상속받는 모든 것들이 구현을 해야하는 메소드
abstract class User {
constructor (
private firstName : string,
private lastName : string,
protected nickname : string,
) {}
abstract getNickName() : void
getFullName() {
return `${this.firstName} ${this.lastName}`
}
}
class Player extends User {
getNickName() {
console.log(this.nickname)
}
}
const mindu = new Player("mindy", "k", "minduu");
mindu.getNickName();
📌접근 가능한 위치
public: 모든 클래스에서 접근 가능
private: 해당 클래스 내에서만 접근 가능 (자식 클래스에서도 접근 불가)
protected: 해당 클래스와 자식 클래스에서 접근 가능
728x90
'Frontend > TypeScript' 카테고리의 다른 글
[TypeScript] 타입 VS 인터페이스 (0) | 2023.01.31 |
---|---|
[TypeScript] 인터페이스 (Interface) (0) | 2023.01.30 |
[TypeScript] 제네릭 (Generic) (0) | 2023.01.28 |
[TypeScript] 타입스크립트 타입 지정 방법 및 타입 종류 (0) | 2023.01.27 |
[TypeScript] 타입 스크립트 기본 (0) | 2022.12.15 |