On dependent properties: Is this poor Object Oriented Programming?
Afficher commentaires plus anciens
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,
Réponse acceptée
Plus de réponses (1)
Guillaume
le 18 Août 2014
0 votes
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 commentaires
Patrick Mboma
le 18 Août 2014
Actually the method you call needs to be protected
classdef C1
properties (Dependent)
d;
end
methods
function value = get.d(this)
value = getdimplementation(this);
end
end
methods (Access = protected)
function value = getdimplementation(this)
%actual implementation of dependent property for C1
end
end
end %end of C1 definition
classdef C2 < C1
methods (Access = protected)
function value = getdimplementation(this)
%actual implementation of dependent property for C2
end
end
end
Patrick Mboma
le 18 Août 2014
per isakson
le 25 Août 2014
Modifié(e) : per isakson
le 25 Août 2014
"There is nowhere I can see"   Use the "debugger" to step through the code and you will see that it works automagically.
Catégories
En savoir plus sur Configure and View Diagnostics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!