abstract class User {
constructor(
private firstName: string,
private lastName: string,
public nickname: string
) {}
}
class Player extends User {
}
const nico = new User("nico", "las", "니꼬");
abstract class User {
constructor(
private firstName: string,
private lastName: string,
public nickname: string
) {}
getFullName() {
return `${this.firstName} ${this.lastName}`
}
}
class Player extends User {
}
const nico = new Player("nico", "las", "니꼬");
nico.getFullName()
abstract class User {
constructor(
protected firstName: string,
protected lastName: string,
protected nickname: string
) {}
// 추상 메서드 : 구현되어 있지 않은 메서드
abstract getNickname():void
getFullName() {
return `${this.firstName} ${this.lastName}`
}
}
class Player extends User {
getNickname() {
console.log(this.nickname)
}
}
const nico = new Player("nico", "las", "니꼬");
nico.getFullName()
private : 인스턴스 밖에서 접근할 수 없고, 다른 자식 클래스에서도 접근할 수 없다.
protected : 클래스 밖에서는 접근할 수 없다.
private, public은 property 뿐만 아니라 메서드에서도 작동한다
abstract class User {
constructor(
private firstName: string,
private lastName: string,
public nickname: string
) {}
private getFullName() {
return `${this.firstName} ${this.lastName}`
}
}
class Player extends User {
}
const nico = new Player("nico", "las", "니꼬");
nico.getFullName()
"use strict";
class User {
constructor(firstName, lastName, nickname) {
this.firstName = firstName;
this.lastName = lastName;
this.nickname = nickname;
}
getFullName() {
return `${this.firstName} ${this.lastName}`;
}
}
class Player extends User {
}
const nico = new Player("nico", "las", "니꼬");
nico.getFullName();
type Words = {
[key:string]: string
}
class Dict {
// words가 private이므로 dictionary 안에서만 words를 보기 원한다.
private words: Words
constructor() {
// 수동으로 초기화 시켜줬음
this.words = {}
}
// 클래스를 타입처럼 사용함
add(word:Word) {
// 주어진 단어가 아직 사전에 존재하지 않을 때
if(this.words[word.term] === undefined) {
this.words[word.term] = word.def;
}
}
def(term:string) {
return this.words[term]
}
}
class Word {
constructor(
public term:string,
public def: string
) {}
}
const kimchi = new Word("kimchi", "한국의 음식");
const dict = new Dict()
dict.add(kimchi);
dict.def("kimchi")