패스트캠퍼스 데브캠프
김민태의 데브캠프 2기 - 접근 제어자
vitamin3000
2024. 11. 11. 12:01
이번 시간에는, class내에서 선언된 변수에 대해 접근을 설정할 수 있는 접근 제어자에 대해 배워보고자 한다.
우선 접근 제어자에는 3가지가 있다.
1. public : 어디서나 접근 가능
2. protected : 선언된 클래스 내부, 선언된 클래스를 상속받은 자식 클래스
3. private : 선언된 해당 클래스 내부에서만 사용 가능
선언 예시
class Post {
private text: string = "";
constructor(private id: number, protected title: string){
this.id = id;
this.title = title;
}
getPost(){
return 'postId : ${this.id}, postTitle: ${this.title}'
}
}
constructor(생성자)보다 위에 변수 값으로 또는, 생성자 안에 매개변수로 선언할 수 있다.