abstract class User {
constructor(
private firstName: string,
protected lastName: string,
public nickName: string
) {}
abstract getNickName(): void
getFullName() {
return `${this.firstName} ${this.lastName}`
}
}
class Player extends User {
}
const timothy = new Player("timothy", "kim", "tim");
timothy.lastName;
timothy.nickName
timothy.getFullName();
접근가능여부 | 선언한 클래스 내 | 상속받은 클래스 내 | 인스턴스 |
---|
private | ⭕ | ❌ | ❌ |
protected | ⭕ | ⭕ | ❌ |
public | ⭕ | ⭕ | ⭕ |
예시
type Words = {
[whatever: string]: string
}
class Dict {
private words: Words
constructor() {
this.word = {}
}
add(word: Word) {
if(this.words[word.term] === undefined) {
this.words[word.term] = word.def
}
}
}
class Word {
constructor() {
public term: string,
public def: string
}
}
const kimchi = new Word("kimchi", "한국의 음식");
const dict = new Dict();
dict.add(kimchi);