4.6.2 접근 제어자
Last updated
class User {
private password: string;
constructor (password: string) {
this.password = password;
}
}
const yoonha = new User('486');
console.log(yoonha.password);
// error TS2341: Property 'password' is private and only accessible within class 'User'.class CarOwner extends User {
carId: string;
constructor (password: string, carId: string) {
super(password);
this.carId = carId;
}
setPassword(newPassword: string) {
this.password = newPassword;
// error TS2341: Property 'password' is private and only accessible within class 'User'.
}
}class User {
protected password: string;
constructor (password: string) {
this.password = password;
}
}
class CarOwner extends User {
carId: string;
constructor (password: string, carId: string) {
super(password);
this.carId = carId;
}
setPassword(newPassword: string) {
this.password = newPassword;
// Okay
}
}class User {
constructor (public id: string, private password: string) { }
}class User {
public id: string;
private password: string;
constructor (id: string, password: string) {
this.id = id;
this.password = password;
}
}