impolements러 interface를 받아 class를 만들때 private이 안되는 오류를 보여주셨는데요 interface를 사용하지 않고 그냥 class안에서 type을 지정하는 방법, abstract class를 이용하는 방법 모두 이해됬습니다. 하지만 interface를 사용하면 private, protected 사용이 불가한 것인지 잘 모르겠어서 질문을 올립니다. 구글링해본결과 class내에 속성으로 만들고 getter, setter를 이용하는 것으로 우회하는 방법을 사용하더라구요. ( https://stackoverflow.com/questions/37791947/how-to-define-a-private-property-when-implementing-an-interface-in-typescript ) 이렇게 했을 때 private의 기능인 class 밖에서는 호출 할 수 없다고 위반되는 결과가 나옵니다. 어떤식으로 해결 해야 할까요? interface Interface { readonly a: string; b: number; } class TSClass implements Interface { private readonly _a: string = "init"; get a() { return this._a; } protected _b: number = 1; get b() { return this._b; } set b(v: number) { this._b = v; } c: string = "기본값이 public"; method() { console.log(this._a); console.log(this._b); console.log(this.c); } } class inheritClass extends TSClass { method() { console.log(this._a); // error console.log(this.a); // 가능.. console.log(this._b); console.log(this.b); console.log(this.c); } } new inheritClass()._a; // error new inheritClass().a; // 가능... new inheritClass()._b; // error new inheritClass().b; // 가능.. new inheritClass().c;
교수님께서는 const int& getDay(){ return m_day;} 로 멤버변수인 m_day를 리턴하셨는데, main함수에서 출력할 때는 그냥 void getDay(){ cout<<day<<endl;}로 public함수를 만들고, 메인에서 today.getDay();로 해줘도 되지 않나요? 가령, getDate()같이 멤버변수들을 모두 출력한다고 하였을 때는 cout<<month<<" "<<day<<" '<<year<<endl;로 하는 것이 편리하지 않나요??
안녕하세요. 영한님과 서포터님들 덕분에 즐겁게 공부하고 있습니다. 주문, 주문상품 엔티티 개발 강의를 듣던 중 궁금한 것이 생겼습니다. 강의에서 보시면 클래스 내부에서 생성 메서드 혹은 비즈니스 로직 메서드를 구현하시면서 getter/setter 메서드를 사용하시는데요. 이렇게 클래스 내부에서 사용할 때도 필드에 직접 접근하는 것보다는 getter/setter 메서드를 사용하는 것이 권장되는가요? 단순히 생각했을 때는 불필요한 메서드 호출만 일어나는 것 아닌가해서 질문드립니다.