Notice
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 알고리즘기초주차
- NotionAI
- REACT
- 프론트엔드
- 사전준비
- Ai
- 맥린이
- 항해99
- typeScript
- D반8조
- Expo
- 멍친구
- Programmers
- 스파르타코딩클럽
- 웹개발종합반
- 실전프로젝트
- 팀워크최고
- rn
- TS
- 7기
- 프로그래머스
- 달리기반
- TDD
- ChatGPT
- 코린이
- 필수강의
- 알pdf #파일탐색기미리보기안될때
- ReactNative
- 리액트
- 챗GPT
Archives
- Today
- Total
FrontEnd :-)
Namad TS - 4. Classes And Interfaces ① 본문
4.0 Classes
//추상 클래스는 오직 다른 클래스가 상속받을 수 있는 클래스
//추상 클래스의 인스턴스를 만들 수 없음. new User 불가
//추상 메소드를 만들려면, 메소드를 클래스 안에서 구현해야 함.
abstract class User {
constructor(
private firstName: string,
protected lastName: string,
public nickname: string
){}
abstract getLastName(): void
getFullName(){
return `${this.firstName} ${this.lastName}`
}
}
class Player extends User {
getLastName(){
console.log(this.lastName);
}
}
const gwen = new Player("gwen", "lee", "그웬");
gwen.getFullName();
접근 가능한 위치
구분 | 선언한 클래스 내 | 상속받은 클래스 내 | 인스턴스 |
private | O | X | X |
protected | O | O | X |
public | O | O | O |
4.1 Recap
type Words = {
[key:string]: string
}
class Dict {
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]
}
delete(term:string){
if(this.words[term]){
delete 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");
Update
//단어 정의 수정
updateDef(word: Word) {
if (this.words[word.term]) {
this.words[word.term] = word.def;
}
}
//단어 이름 수정
updateTerm(oldTerm: string, newTerm: string) {
if (this.words.hasOwnProperty(oldTerm)) {
this.words[newTerm] = this.words[oldTerm];
delete this.words[oldTerm];
}
}
기타
//사전에 저장된 단어 개수
size() {
return Object.keys(this.words).length;
}
//사전 전체 출력
all(){
for(let [key, value] of Object.entries(this.words)){
console.log(`${key}: ${value}`)
}
}
단어 정의 여러 개(문자열 배열) 가능하게, 정의 추가/수정/삭제
type Words = {
[key:string]: (string | string[])
}
class Dict {
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]
}
delete(term:string){
if(this.words[term]){
delete this.words[term]
}
}
//단어 정의 수정
update(word: Word) {
if (this.words[word.term]) {
this.words[word.term] = word.def;
}
}
//단어 이름 수정
updateTerm(oldTerm: string, newTerm: string) {
if (this.words.hasOwnProperty(oldTerm)) {
this.words[newTerm] = this.words[oldTerm];
delete this.words[oldTerm];
}
}
//사전에 저장된 단어 개수
size() {
return Object.keys(this.words).length;
}
//사전 전체 출력
all(){
for(let [key, value] of Object.entries(this.words)){
console.log(`${key}: ${value}`)
}
}
}
class Word {
constructor(
public term: string,
public def: (string | string[])
) {}
//단어 정의 추가
addDef(newDef: string) {
if(typeof this.def === 'string'){
this.def = [this.def, newDef]
} else {
this.def = [...this.def, newDef];
}
}
//단어 기존 정의 <=> 새 정의 수정
updateDef(oldDef: string, newDef: string){
console.log("update", this.def, oldDef);
if(typeof this.def === 'string'){
if(oldDef === this.def) {
this.def = newDef
}
} else {
this.def = this.def.filter(v => v !== oldDef);
console.log("filter", this.def);
this.def.push(newDef);
console.log("push", this.def);
}
}
//단어 정의 삭제
deleteDef(oldDef: string){
if(typeof this.def !== 'string'){
this.def = this.def.filter(v => v !=oldDef);
// 정의 하나인 경우 [] => "" 바꿔주기
if(kimchi.def.length === 1){
this.def = this.def.toString();
}
}
}
}
const kimchi = new Word("kimchi", "한국의 음식");
const galbi = new Word("갈비", "고기")
const dict = new Dict();
dict.add(kimchi);
dict.add(galbi);
dict.def("kimchi");
console.log 에서
kimchi.addDef("한국꺼!"); //kimchi.def = ["한국의 음식", "한국꺼!"]
kimchi.updateDef("한국의 음식", "Korean traditional food"); //kimchi.def = ["한국꺼!", "Korean traditional food"]
몇 가지는 커뮤니티 게시판 댓글 참고했고, 몇 가지 더 추가함.
계속 생각할 게 생긴다,, 여기서 일단 마무리!
'TypeScript' 카테고리의 다른 글
Namad TS - 5.Typescript Blockchain (0) | 2023.02.27 |
---|---|
Namad TS - 4. Classes And Interfaces ② (0) | 2023.02.25 |
코딩앙마 타입스크립트 기초 강의 2 (0) | 2022.09.07 |
코딩앙마 타입스크립트 기초강의 (0) | 2022.09.06 |
Comments