getter & setter
getter
class ShoppingMall {
protected employees: string[] = [];
constructor(
private productId: string,
private productName: string,
public productType: string
) {}
printProduct(this: ShoppingMall) {
console.log("productId : " + this.productId);
console.log("productName : " + this.productName);
console.log("productType : " + this.productType);
}
editProductId(this: ShoppingMall) {
this.productId = "PD02";
}
addEmployee(employee: string) {
this.employees.push(employee);
}
}
class Grocery extends ShoppingMall {
private report: string;
constructor(productId: string, private reports: string[]) {
super(productId, "Carrot", "Food");
this.report = reports[0];
}
addEmployee(employee: string) {
this.employees.push(employee);
}
addReport(text: string) {
this.reports.push(text);
this.report = text;
}
}
- 위 코드에서 report 프로퍼티는 private로 선언되었기 때문에 외부에서는 액세스할 수 없다.
- 이 때, getter를 이용하면 외부에서도 report 프로퍼티에 액세스 할 수 있다.
- getter는 값을 가져오기 위해 함수나 메서드를 실행하는 프로퍼티로 getter를 사용하여 복잡한 로직을 구현할 수 있다.
- getter를 사용하는 방법은 get 키워드를 사용한 다음 원하는 이름을 짓고, 일반적인 메서드처럼 구현하면 된다.
class ShoppingMall {
protected employees: string[] = [];
constructor(
private productId: string,
private productName: string,
public productType: string
) {}
printProduct(this: ShoppingMall) {
console.log("productId : " + this.productId);
console.log("productName : " + this.productName);
console.log("productType : " + this.productType);
}
editProductId(this: ShoppingMall) {
this.productId = "PD02";
}
addEmployee(employee: string) {
this.employees.push(employee);
}
}
class Grocery extends ShoppingMall {
private report: string;
get getRecentReport() {
return this.report;
}
constructor(productId: string, private reports: string[]) {
super(productId, "Carrot", "Food");
this.report = reports[0];
}
addEmployee(employee: string) {
this.employees.push(employee);
}
addReport(text: string) {
this.reports.push(text);
this.report = text;
}
}
- 28~30번째 줄의 코드와 같이 구현하면 되는데, 이렇게 작성하면 어디에서나 report에 대해 액세스 할 수 있기 때문에 더 복잡한 로직을 구현하는 것이 좋다.
class ShoppingMall {
protected employees: string[] = [];
constructor(
private productId: string,
private productName: string,
public productType: string
) {}
printProduct(this: ShoppingMall) {
console.log("productId : " + this.productId);
console.log("productName : " + this.productName);
console.log("productType : " + this.productType);
}
editProductId(this: ShoppingMall) {
this.productId = "PD02";
}
addEmployee(employee: string) {
this.employees.push(employee);
}
}
class Grocery extends ShoppingMall {
private report: string;
get getRecentReport() {
if (this.report) {
return this.report;
}
throw new Error("NO REPORT");
}
constructor(productId: string, private reports: string[]) {
super(productId, "Carrot", "Food");
this.report = reports[0];
}
addEmployee(employee: string) {
this.employees.push(employee);
}
addReport(text: string) {
this.reports.push(text);
this.report = text;
}
}
const grocery = new Grocery("PD01", []);
grocery.addReport("Report 01");
console.log(grocery.getRecentReport);
- report가 없을 때에는 오류를 던지도록 로직을 수정하였고, addReport 메서드를 이용하여 "Report 01"을 추가하였다.
- 53번째 줄과 같이 getter를 사용할 때는 일반적인 프로퍼티를 액세스할때와 같이 사용하면 된다.
- 콘솔 로그의 결과 정상적으로 출력되는 것을 볼 수 있다.
setter
- setter 또한 getter와 유사하게 set 키워드를 사용하고, 뒤에 원하는 이름을 붙여서 사용한다.
class ShoppingMall {
protected employees: string[] = [];
constructor(
private productId: string,
private productName: string,
public productType: string
) {}
printProduct(this: ShoppingMall) {
console.log("productId : " + this.productId);
console.log("productName : " + this.productName);
console.log("productType : " + this.productType);
}
editProductId(this: ShoppingMall) {
this.productId = "PD02";
}
addEmployee(employee: string) {
this.employees.push(employee);
}
}
class Grocery extends ShoppingMall {
private report: string;
get getRecentReport() {
if (this.report) {
return this.report;
}
throw new Error("NO REPORT");
}
set setRecentReport(val: string) {
if (!val) {
throw new Error("NO REPORT PASSED");
}
this.addReport(val);
}
constructor(productId: string, private reports: string[]) {
super(productId, "Carrot", "Food");
this.report = reports[0];
}
addEmployee(employee: string) {
this.employees.push(employee);
}
addReport(text: string) {
this.reports.push(text);
this.report = text;
}
}
const grocery = new Grocery("PD01", []);
grocery.addReport("Report 01");
console.log(grocery.getRecentReport);
- 35~41번째 줄에서와 같이 setRecentReport()를 사용하면 addReport를 호출하도록 코드를 작성하였다.
class ShoppingMall {
protected employees: string[] = [];
constructor(
private productId: string,
private productName: string,
public productType: string
) {}
printProduct(this: ShoppingMall) {
console.log("productId : " + this.productId);
console.log("productName : " + this.productName);
console.log("productType : " + this.productType);
}
editProductId(this: ShoppingMall) {
this.productId = "PD02";
}
addEmployee(employee: string) {
this.employees.push(employee);
}
}
class Grocery extends ShoppingMall {
private report: string;
get getRecentReport() {
if (this.report) {
return this.report;
}
throw new Error("NO REPORT");
}
set setRecentReport(val: string) {
if (!val) {
throw new Error("NO REPORT PASSED");
}
this.addReport(val);
}
constructor(productId: string, private reports: string[]) {
super(productId, "Carrot", "Food");
this.report = reports[0];
}
addEmployee(employee: string) {
this.employees.push(employee);
}
addReport(text: string) {
this.reports.push(text);
this.report = text;
}
}
const grocery = new Grocery("PD01", []);
grocery.setRecentReport = "Report 02";
console.log(grocery.getRecentReport);
- setter를 사용할 때는 메서드처럼 사용하지 않고, 프로퍼티를 액세스하듯이 사용한다.
- 콘솔 로그의 결과 setter가 정상적으로 작동한 것을 확인할 수 있다.
'Typescript' 카테고리의 다른 글
추상화(abstract) (0) | 2025.07.06 |
---|---|
정적 메서드 (0) | 2025.07.06 |
상속, protected (0) | 2025.07.03 |
클래스 (1) | 2025.06.26 |
함수 반환 타입, 타입 기능 함수 (1) | 2025.06.24 |