On dependent properties: Is this poor Object Oriented Programming?
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi, I have a class C1 with a dependent property d and another class C2 which inherits from C1.
When property d is called from C2, it naturally goes through C1. I would like the d coming from C2 to be a transformation of the d coming from C1.
What I did is to create an auxiliary function (not a new method) that is called by C1. The function does what C1 would do to get d and then checks whether the input is a C2 object in which case it transforms d.
Is this bad programming? Would you do it differently?
Thanks,
댓글 수: 0
채택된 답변
Adam
2014년 8월 18일
편집: Adam
2014년 8월 19일
I would usually use the idea of a protected function that extends the base class.
So the base class function is of the type:
function d = get.d( obj )
% base class implementation here
d = doGetD( obj, d )
end
with an empty protected implementation of doGetD in the base class and an implementation that extends the base class in the derived class.
댓글 수: 2
추가 답변 (1개)
Guillaume
2014년 8월 18일
No, it's not very good as your base class C1 now contains implementation that belongs to the derived class C2.
Rather than calling an auxiliary function in C1 d getter, you should call a private class method, that you then override in class C2.
댓글 수: 4
per isakson
2014년 8월 25일
편집: per isakson
2014년 8월 25일
"There is nowhere I can see"   Use the "debugger" to step through the code and you will see that it works automagically.
참고 항목
카테고리
Help Center 및 File Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!