필터 지우기
필터 지우기

Overwriting a setter or getter in a subclass: why is this not possible???

조회 수: 33 (최근 30일)
Michael Dzjaparidze
Michael Dzjaparidze 2012년 2월 1일
답변: Damien Watson 2023년 10월 17일
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
Michael Dzjaparidze
Michael Dzjaparidze 2012년 2월 1일
I want to be able to re-define a setter method originally defined in some superclass in a subclass. Just as you would be able to do in objective c for instance. Do you mean you want to have more details specifically about what I am trying to implement?
John
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
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.

Damien Watson
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.

카테고리

Help CenterFile 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!

Translated by