매개변수 프로퍼티

생성자의 매개변수에 접근 제어자(public, private, protected, readonly)를 추가해
클래스의 프로퍼티를 자동으로 선언 및 초기화하는 기능

class Food {
  name: string;
  kcal: number;

  constructor(name: string, kcal: number) {
    this.name = name;
    this.kcal = kcal;
  }
}

접근자

객체의 멤버에 대한 접근을 가로채는 방식으로 getters/setters를 지원

class Hero {
  private _name: string;

  constructor(name: string) {
    this._name = name;
  }

// getter
  public get name(): string {
    return this._name;
  }

// setter
  public set name(newName: string) {
    if (newName.length > 0) {
      this._name = newName;
    } else {
      throw new Error('Name cannot be empty');
    }
  }
}

const hero = new Hero('Iron Man');
console.log(hero.name); // Iron Man
hero.name = 'Spider Man'; // Setter 호출
console.log(hero.name); // Spider Man

전역 프로퍼티

특정 스코프에 속하지 않고 어디서든 접근 가능한 변수를 의미

class MyStaticClass {
  static staticProperty: string = "I am static!";

  static staticMethod() {
    console.log(`Static Property: ${this.staticProperty}`);
  }
}

// Accessing static properties and methods
console.log(MyStaticClass.staticProperty); // "I am static!"
MyStaticClass.staticMethod(); // "Static Property: I am static!"

추상 메서드

abstract 키워드 사용
abstract 키워드로 클래스 또는 메서드를 선언

abstract class Character {
  abstract action(): void; // 추상 메서드 (구현 없음)

  move(): void {
    console.log("Moving...");
  }
}

class Archor extends Character {
  action(): void {
    console.log("attack!");
  }
}

const archor = new Archor();
archor.action(); // "attack!"
archor.move(); // "Moving..."

// const character = new Character(); // 에러: 추상 클래스는 인스턴스화할 수 없습니다.

생성자 함수

클래스의 인스턴스를 초기화하기 위해 호출되는 특수한 메서드
constructor 키워드를 사용하여 정의
객체 생성 시 필요한 초기값을 설정 및 초기 로직을 실행

특징
클래스의 인스턴스 생성 시 자동 호출
단 한 번만 실행되며, 객체 초기화 역할
매개변수를 통해 인스턴스를 생성할 때 필요한 값을 전달받음

class Stone {
  name: string;
  hardness: string;

  constructor(name: string, hardness: string) {
    this.name = name; // 인스턴스 변수 초기화
    this.hardness = hardness;
  }

  StoneInfo(): void {
    console.log(`stone : ${this.name} , hardness : ${this.hardness}.`);
  }
}

const myStone = new Stone("Garnet", "7.0"); // 생성자 호출
myStone.StoneInfo(); // stone : Garnet , hardness : 7.0

 

'TS' 카테고리의 다른 글

열거형 (2)  (0) 2024.12.05
열거형 (1)  (0) 2024.12.04
클래스 (1)  (0) 2024.12.02
교차 타입  (0) 2024.11.29
리터럴 타입  (0) 2024.11.26

클래스

TypeScript 클래스는 객체지향 프로그래밍(OOP)을 지원하기 위해 제공되는 기능

 

기본 선언

class Architecture {
  name: string; // 클래스 속성
  year: number;

  constructor(name: string, year: number) { // 생성자
    this.name = name;
    this.year = year;
  }

  profile(): string { // 메서드
    return ` 건물명 : ${this.name}, 건물년수 : ${this.year} `;
  }
}

// 클래스 사용
const architecture = new Architecture("63빌딩", 39);
console.log(architecture.profile()); // Hello, my name is Alice and I am 30 years old.

console.log(Inheritance);

상속

이미 존재하는 클래스를 확장해 새로운 클래스를 만드는 기능

class Gun {
  constructor(public name: string) {}

  reload(): void {
    console.log(`${this.name}이 장전 되었습니다.`);
  }
}

class gun extends Gun {
  fire(): void {
    console.log(`${this.name}이 발사 되었습니다.`);
  }
}

// 사용 예시
const pistol = new gun("glock");
pistol.reload(); // glock이 장전 되었습니다.
pistol.fire(); // glock이 발사 되었습니다.

접근 제어자

 

1. public

어디서나 접근 가능.
명시적으로 선언하지 않으면 모든 멤버는 기본적으로 public으로 설정(기본 값)

class PublicExample {
    public name: string; // public 생략 가능
    constructor(name: string) {
        this.name = name;
    }
}
const PublicEx = new PublicExample("John");
console.log(PublicEx.name); // 접근 가능

 

2. private
클래스 내부에서만 접근 가능.
클래스 외부나 상속받은 클래스에서도 접근 불가

class PrivateExample {
    private secret: string;
    constructor(secret: string) {
        this.secret = secret;
    }
    getSecret() {
        return this.secret; // 내부에서 접근 가능
    }
}
const PrivateEx = new PrivateExample("hidden");
console.log(PrivateEx.getSecret()); // 접근 가능
// console.log(ex.secret); // 오류: private 속성

 

3. protected

클래스 내부와 상속받은 클래스에서 접근 가능
클래스 외부에서는 접근 불가

class Parent {
    protected value: number;
    constructor(value: number) {
        this.value = value;
    }
}
class Child extends Parent {
    getValue() {
        return this.value; // 상속받은 클래스에서 접근 가능
    }
}
const child = new Child(42);
console.log(child.getValue()); // 접근 가능
// console.log(child.value); // 오류: protected 속성

 

'TS' 카테고리의 다른 글

열거형 (1)  (0) 2024.12.04
클래스 (2)  (0) 2024.12.03
교차 타입  (0) 2024.11.29
리터럴 타입  (0) 2024.11.26
함수(Function)[2]  (0) 2024.11.25

+ Recent posts