// class
// 접근 제어자(Access Modifiers)
// public - 어디서나 자유롭게 접근 가능, 클래스 바디에서 생략 가능
// protected - 나와 파생된 후손 클래스 내에서 접근 가능
// private - 내 클래스에서만 접근 가능
class UserA {
// 바디 안에 this로 접근하는 속성은 위쪽에서 만들어 줄 수 있다.
// 에러가 사라짐
// public 접근 제어자가 숨겨져 있는데, 붙여주자.
// 단, Constructor에서는 public을 사용해 줘야 한다.
constructor(
public first: string,
protected last: string,
private age: number
){
}
protected getAge() { // 속성이 보호되고 있다.
return `${this.first} ${this.last} is ${this.age}`
}
}
class UserB extends UserA{
getAge() {
return `${this.first} ${this.last} is ${this.age}` // private이기 때문에 에러 발생
}
}
class UserC extends UserB{
getAge() {
return `${this.first} ${this.last} is ${this.age}`
}
}
const neo = new UserA('neo', 'aads', 123);
console.log(neo.first)
console.log(neo.last) // 보호된 속성일 경우 에러 발생
console.log(neo.age)
neo.getAge() // 보호되고 있어서 에러 발생
참조