Typescript_스터디 13-14일차 (23/02/18-19)

yhwa·2023년 2월 17일
0
post-thumbnail

📝 Typescript로 블록체인 만들기 : From #4.2 to #4.5

Typescript를 이용한 객체지향 프로그래밍

- 추상 클래스, 다형성, 제네릭 타입과 같은 기능도 TS에서 사용할 수 있습니다.
- TS에서 많은 양의 반복 코드를 줄이는 방법을 알아봅니다.
- 모든 보호 기능은 TS에서 작동합니다.

TS에서 클래스(Class) 만들기

- 클래스 안에 constructor함수를 작성하고, 생성된 객체의 프로퍼티를 작성합니다.
- constructor는 빈 객체를 생성하는 역할을 하며, constructor 내부 프로퍼티는 
- 해당 객체의 키과 값을 의미합니다.
- JS보다 TS에서의 작성법이 더 단축됩니다. TS에서는 private,public,protected 키워드와
- 필드명, 타입만 인자(argument)자리에 작성하면 객체 생성 완성입니다.
- private, public, protected는 대부분의 객체지향 프로그래밍에서 보여지는 특징입니다.
- 이 키워드 또한 컴파일후, JS에서는 보이지 않습니다.
// TS class_constructor 객체 생성
class Student{
	constructor(
  		private firstName:string,
        private lastName:string,
        public nickName:string
  ){}
}
//JS class_constructor 객체 생성
class Student{
	constructor(firstName, lastName, nickName){
    	this.firstName = firstName,
        this.lastName = lastName,
        this.nickName = nickName
    }
}

인스턴스 (Instance) 생성

 - 클래스를 기반으로 인스턴스를 생성할 수 있습니다.
 - private 키워드를 가진 프로퍼티는 클래스 밖에서 사용될 수 없습니다.
 - public 키워드는 클래스 밖에서도 사용됩니다.
class Student{
	constructor(
  		private firstName:string,
        private lastName:string,
        public nickName:string
  ){}
}

const harry = new Student("harry","potter","hp");
/* Student: {
  "firstName": "harry",
  "lastName": "potter",
  "nickName": "hp"
} */

harry.firstName(); // Error
harry.nickName(); // hp

추상 클래스 (Abstract Class)

- TS와 객체지향 프로그래밍의 가장 큰 장점은, 추상 클래스입니다.
- 추상 클래스는 다른 일반 클래스가 상속받을 수 있는 클래스입니다.
- 추상 클래스는 인스턴스를 만들수 없습니다.
//abstract class
abstract class User {
	constructor(
  		private firstName:string,
        private lastName:string,
        public nickName:string
  ){}
}

//class
class Student extends User {
	
}

//instance
const harry = new Student("harry","potter","hp");

추상 클래스 내부 메소드 (Method)

- 추상클래스 내부에 getFullName이라는 메소드를 작성하여, 풀네임 string을 반환합니다.
- 메소드 앞에도 private/public/protected 키워드를 사용하여 정보를 보호할 수 있습니다.
- JS으로 컴파일되면 해당 키워드들이 사라집니다.
abstract class User {
	constructor(
  		private firstName:string,
        private lastName:string,
        public nickName:string
  ){}
  
  //추상클래스 내부 메소드
  getFullName(){
  	return `${this.firstName} ${this.lastName}`;
  }
}

class Student extends User {}

const harry = new Student("harry","potter","hp");
console.log(harry.getFullName()); // harry potter

추상 메소드 (Abstract Method)

- 추상 메소드는 선언부와 구현부가 따로 나누어 작성하도록 되어있습니다.
- 타입에 대한 추상 메소드의 선언은 추상 클래스 내부에서 하며,
- 그 메소드에 대한 구현체는 상속되는 일반클래스에서 작성합니다.
- private 키워드는 클래스 밖에서 사용할 수 없으나, protected는 
- 클래스 밖에서도 사용가능하며, 수정만 불가하도록 데이터를 보호합니다.
- 추상 메소드는 추상 클래스 영역에서는 call signature만 가집니다.
- 추상 메소드를 사용하면 상속 클래스부에 메소드에 대한 구현부 작성을 강제합니다.
- 추상 메소드는 추상 클래스를 상속 받은 모든 것들이 구현을 해야하는 메소드입니다. (구현을 강제)
- 필드를 보호하기위한 키워드 private, public, protected ...(접근제어자)
- private 키워드를 가진 프로퍼티는 인스턴스 밖에서 접근할수 없으며, 다른 자식 
- 클래스에서도 접근할 수 없습니다.
- private은 개인적임을 의미하며 User클래스의 인스턴스나 메소드에서
- 접근할 수 있으나, User클래스는 추상클래스라 인스턴스화 할 수 없습니다.
- 필드가 외부로부터 보호되지만, 해당 프로퍼티가 다른 자식 클래스에서 사용되길 원한다면, 
- private이 아닌, protected를 사용합니다.
- 클래스 밖에서는 protected 프로퍼티에 접근할 수 없습니다.
abstract class User {
	constructor(
  		protected firstName:string,
        protected lastName:string,
        public nickName:string
  ){}
  
  abstract getFullName():string;
}

class Student extends User {
	getFullName(){
    	return `${this.firstName} ${this.lastName}`;
    }
}

const harry = new Student("harry","potter","hp");

해시맵 만들기

- 해싱 알고리즘을 쓰는 해시맵입니다.
- 단어 사전으로 구현합니다.
- 사전이 words 오브젝트를 가집니다.
- 사전에 단어를 새로 추가하고, 단어의 정의를 찾고 단어를 삭제하는 메소드를 만듭니다.
profile
📌 FE 공부 정리 공간입니다.

0개의 댓글