📘 TypeScript
TypeScript - 클래스의 접근 제어자
zunwon
2023. 11. 21. 18:37
클래스의 접근 제어자
public
- 어디서나 해당하는 속성을 자유롭게 접근해서 사용할수 있다
- 생략 가능, Default 값
class Animal {
public name: string;
public sound: string;
constructor(name: string, sound: string) {
this.name = name;
this.sound = sound;
}
}
class Dog extends Animal {
public color: string;
constructor(name: string, sound: string, color: string) {
super(name, sound);
this.color = color;
}
speak() {
return this.sound;
}
getColor() {
return this.color;
}
}
const dog = new Dog("휜둥이", "멍멍!", "white");
dog.speak(); // '멍멍'
dog.getColor(); // 'white
dog.name; // '흰둥이'
dog.sound; // '멍멍!'
dog.color; // 'white
protected
- 해당하는 클래스와 클래스가 확장된 다른 클래스 안에서만 접근
class Animal {
protected name: string;
public sound: string;
constructor(name: string, sound: string) {
this.name = name;
this.sound = sound;
}
}
class Dog extends Animal {
public color: string;
constructor(name: string, sound: string, color: string) {
super(name, sound);
this.color = color;
}
speak() {
return this.sound;
}
getColor() {
return this.color;
}
}
const dog = new Dog("휜둥이", "멍멍!", "white");
dog.speak(); // '멍멍'
dog.getColor(); // 'white
dog.name; // 에러 발생
dog.sound; // '멍멍!'
dog.color; // 'white
private
- 해당하는 클래스 외에서는 그 속성을 접근할 수 없다
class Animal {
private name: string;
public sound: string;
constructor(name: string, sound: string) {
this.name = name;
this.sound = sound;
}
}
class Dog extends Animal {
public color: string;
constructor(name: string, sound: string, color: string) {
super(name, sound);
this.color = color;
}
speak() {
console.log(this.name); // 에러 (비공개)
return this.sound;
}
getColor() {
return this.color;
}
}
const dog = new Dog("휜둥이", "멍멍!", "white");
dog.speak(); // '멍멍'
dog.getColor(); // 'white
dog.name; // 에러 (비공개)
dog.sound; // '멍멍!'
dog.color; // 'white
클래스 기타 수식어
static
- 정적 메서드를 만들 수 있다
- 접근 제어자 뒤쪽에서 사용해야 한다 (앞쪽에 작성하면 에러 발생!)
class Animal {
private name: string;
public sound: string;
constructor(name: string, sound: string) {
this.name = name;
this.sound = sound;
}
public static speak() {
console.log('Animal speak!');
}
}
class Dog extends Animal {
public color: string;
constructor(name: string, sound: string, color: string) {
super(name, sound);
this.color = color;
Animal.speak();
}
getColor() {
return this.color;
}
}
Animal.speak();
readonly
- 읽기 전용
- 속성에만 적용하고 메서드에는 적용하지 않는다
- 최초 초기화하는 코드 제외하면 새롭게 할당할 수 없다
class Animal {
private name: string;
public readonly sound: string;
constructor(name: string, sound: string) {
this.name = name;
this.sound = sound;
}
public static speak() {
console.log("Animal speak!");
}
}
class Dog extends Animal {
public color: string;
constructor(name: string, sound: string, color: string) {
super(name, sound);
this.color = color;
this.sound = "야옹" // readonly 속성이기 때문에 에러 발생
Animal.speak();
}
getColor() {
return this.color;
}
}
수식어 축약
class Animal {
private name: string;
public readonly sound: string;
constructor(name: string, sound: string) {
this.name = name;
this.sound = sound;
}
}
// 수식어 축약
class Animal {
constructor(private name: string, public readonly sound: string) {}
}