typescript

23.01.29 Access Modifier

슈팅스타제제 2023. 1. 30. 01:31

클래스에서 사용할 수 있는 access modifier 접근 제한자에는 3가지 종류가 있다. 

public, private, protected

접근 가능성 public protected private
클래스 내부 O O O
자식 클래스 내부 O O X
클래스 인스턴스 O X X

 

클래스 인스턴스를 통해 외부 참조가 가능한 것은 public 만 해당된다. 

class A {
    public a: string;
    protected b: string;
    private c: string;
}

const A = new A('a', 'b', 'c');
console.log('public 가능', A.a);

 

자식 클래스를 통해 내부 참조가 가능한 것은 public 과 protected 둘만 해당된다.

class B extends A {
    super(a, b, c);
    console.log('public 가능!', this.a);
    console.log('protected 가능!', this.b);
}

셋 다 클래스 내부에서는 참조 가능하다~!

class A {
    public a: string;
    protected b: string;
    private c: string;
    
    constructor (a: string, b: string, c: string){
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

 

 

참고문서 링크 

https://velog.io/@wjd489898/Typescript-%ED%81%B4%EB%9E%98%EC%8A%A4Class-private-protected-public

 

[Typescript] 클래스(Class) : private, protected, public

클래스 기반 객체 지향 언어가 지원하는 접근 제한자(Access modifier) public,private,protected를 지원하며 의미 또한 동일하다. 접근 제한자를 명시하지 않았을 때 \- 다른 클래스 기반 언어 : protected로 지

velog.io

 

'typescript' 카테고리의 다른 글

23.02.05 왜 super call?  (0) 2023.02.05
23.01.29 Union Type  (0) 2023.01.29
22.02.24 default parameter  (0) 2022.02.24
22.02.18 interface extends 안될 때, Omit  (0) 2022.02.20
21.12.23 [typescript] setState props로 넘길 때 Dispatch  (1) 2021.12.23