Overwriting a setter or getter in a subclass: why is this not possible???
31 次查看(过去 30 天)
显示 更早的评论
This is a real essential thing to be able to do in a project I'm working on at the moment, but it appears this is not allowed in MATLAB for some strange reason...
Is there a reason for this I cannot think of, or is it just a limitation of OOP in MATLAB?
classdef SubClass < SuperClass
methods
function obj = set.SomePropDefinedInTheSuper(obj,newVal)
end
end
end
3 个评论
John
2012-5-22
It is mentioned in the Matlab documentation that modifying setters and getters in a subclass are not possible. I also do not understand why. It would be a useful feature in a future release.
From the documentation:<http://www.mathworks.nl/help/techdoc/matlab_oop/brenyev-1.html#brenyev-6>
Modifying Superclass Properties
There are two separate conditions under which you can redefine superclass properties:
The value of the superclass property Abstract attribute is true
The values of the superclass property SetAccess and GetAccess attributes are private
In the first case, the superclass is just requesting that you define a concrete version of this property to ensure a consistent interface. In the second case, only the superclass can access the private property, so the subclass is free to reimplement it in any way.
回答(2 个)
Daniel Shub
2012-5-22
I don't know much about OOP concepts or implementations in any language (including MATLAB), but I think in general there is confusion about the differences between properties and methods. From my understanding languages that support OOP, including MATLAB, make it trivial to overload methods. Overloading properties tends to be a pain. I think in Python you can, but it is a real pain. I am not sure if you can in C or C#. In MATLAB you cannot do it, but you can work around it in at least two ways.
The first thing you should ask yourself is do you want to overload the property or the method? This is in fact the first work around. Have the superclass have a property and a set method for the property. This is not the set.myProp function, but rather a method you name and define (possibly setMyProp). The setMyProp method would then set the myProp property. This would allow a subclass to overload the setMyProp method.
You could create a new property mySubProp in the subclass and overload subsref to intercept calls to obj.myProp and route them to obj.mySubProp. This can be a pain given how subsref works within methods of the class.
0 个评论
Damien Watson
2023-10-17
For anyone coming across this thread now, this can be done by editting the superclass to allow it eg:
classdef SuperClass
properties
MyProp
end
methods
function value = get.MyProp(obj)
% Getter simply calls overridable function
value = obj.GetMyProp;
end
function value = GetMyProp(obj)
value = obj.MyProp;
end
end
end
Then, in the subclass, simply override the method that you made the getter call.
classdef SubClass < SuperClass
methods
function value = GetMyProp(obj)
value = 0;
end
end
end
Now, your subclass will always show 0 for MyProp, while the superclass keeps the correct value.
0 个评论
另请参阅
类别
在 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!