Skip to content

Latest commit

 

History

History
197 lines (147 loc) · 4.42 KB

File metadata and controls

197 lines (147 loc) · 4.42 KB

2. 접근 제어자

접근 제어자

접근 제어자는 TS에서만 제공되는 기능으로 클래스의 특정 필드나 메서드를 접근할 수 있는 범위를 설정하는 기능임


TS에서는 다음과 같은 3개의 접근 제어자를 사용할 수 있음

  • public: 모든 범위 내에서 접근 가능
  • private: 클래스 내부에서만 가능
  • protected: 클래스 내부 또는 파생 클래스 내부에서만 가능

Public

공공이라는 뜻으로 어디서든지 이 프로퍼티에 접근할 수 있음을 의미함.

  • 필드의 접근 제어자를 지정하지 않으면 기본적으로 public 접근 제어자를 갖게 됨
class Employee {
  // 필드
  name: string;      // 자동으로 public
  age: number;       // 자동으로 public
  position: string;  // 자동으로 public

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

  // 메서드
  work() {
    console.log("일함");
  }
}

const employee = new Employee("이정환", 27, "devloper");

employee.name = "홍길동";
employee.age = 30;
employee.position = "디자이너";

다음과 같이 pulic 접근 제어자를 직접 명시하는 것도 가능함

class Employee {
  // 필드
  public name: string;
  public age: number;
  public position: string;

  ...
}

const employee = new Employee("이정환", 27, "devloper");

employee.name = "홍길동";
employee.age = 30;
employee.position = "디자이너";

private

제한된, 사적인이라는 뜻으로 특정 필드나 메서드의 접근 제어자를 private으로 설정하면 클래스 내부에서만 이 필드에 접근할 수 있게 됨

class Employee {
  // 필드
  private name: string; // private 접근 제어자 설정
  public age: number;
  public position: string;

  ...

  // 메서드
  work() {
    console.log(`${this.name}이 일함`); // 여기서는 접근 가능
  }
}

const employee = new Employee("이정환", 27, "devloper");

employee.name = "홍길동"; // ❌ 오류
employee.age = 30;
employee.position = "디자이너";

Protected

private과 public의 중간으로 클래스 외부에서 접근이 안되지만 클래스 내부와 파생 클래스에서 접근 가능하도록 설정하는 접근 제어자

class Employee {
  // 필드
  private name: string; // private 접근 제어자 설정
  protected age: number;
  public position: string;

  ...

  // 메서드
  work() {
    console.log(`${this.name}이 일함`); // 여기서는 접근 가능
  }
}

class ExecutiveOfficer extends Employee {
 // 메서드
  func() {
    this.name; // ❌ 오류 
    this.age; // ✅ 가능
  }
}

const employee = new Employee("이정환", 27, "devloper");

employee.name = "홍길동"; // ❌ 오류
employee.age = 30; // ❌ 오류
employee.position = "디자이너";

필드 생략하기

접근 제어자는 다음과 같이 생성자의 매개변수에도 설정할 수 있음

class Employee {
  // 필드
  private name: string;    // ❌
  protected age: number;   // ❌
  public position: string; // ❌

  // 생성자
  constructor(
    private name: string,
    protected age: number,
    public position: string
  ) {
    this.name = name;
    this.age = age;
    this.position = position;
  }

  // 메서드
  work() {
    console.log(`${this.name} 일함`);
  }
}

그러나 생성자에 접근 제어자를 설정하면 동일한 이름의 필드를 선언하지 못하게됨.

  • 생성자 매개변수에 name, age, position 처럼 접근 제어자가 설정되면 자동으로 필드도 함께 선언되기 때문문
  • 따라서 동일한 이름으로 필드를 중복 선언할 수 없게 됨.

따라서 중복된 필드 선언을 모두 제거해 주어야 함

class Employee {
  // 생성자
  constructor(
    private name: string,
    protected age: number,
    public position: string
  ) {
    this.name = name;
    this.age = age;
    this.position = position;
  }

  // 메서드
  work() {
    console.log(`${this.name} 일함`);
  }
}

또 다음과 접근 제어자가 설정된 매개변수들은 this.필드 = 매개변수 가 자동으로 수행됨. 따라서 위 코드의 name, age, position은 모두 this 객체의 프로퍼티 값으로 자동 설정되기 때문에 다음과 같이 생성자 내부으 ㅣ코드를 제거해도 됨