How can I call the set-dot method on a subclass object to set a property of the superclass?

조회 수: 4 (최근 30일)
I have a superclass, and a subclass that inherits superclass. "xyz" property is defined in superclass. Superclass inherits "handle" class. I am trying to create a "set.xyz(obj)" method in subclass so that when "xyz" is being set, it calls that function but I get an error saying it cannot do that because "xyz" is in the superclass. Also, I have another method "update" in the subclass that I want to be executed whenever I set the "xyz" property by calling the set-dot method on the subclass object. Ideally I would like to be able to execute the following commands:
m = MySubClass;
m.xyz = 'a'; % this also calls an "update" method of the subclass
How can I call the set-dot method on a subclass object to set a property of the superclass?

채택된 답변

MathWorks Support Team
MathWorks Support Team 2018년 3월 9일
You cannot define a "set.xyz" method in a subclass to set the "xyz" property of the superclass. This restriction prevents a subclass from overriding a superclass implementation and cannot be bypassed. However, in some situations superclass property needs to grant subclasses the ability to participate in the setting or getting of a property value, like in this case. To do so, a superclass "set" function can call a protected method to allow subclasses to participate in property access while still maintaining some level of control.
So the implementation of the two classes might look like this:
classdef MySuperClass < handle
properties
xyz
end
methods
function set.xyz(obj,value)
disp(['setting xyz in MySuperClass to ', value]);
obj.xyz = value; % updates the property
respondToXYZChange(obj) % calls the protected method
end
end
methods(Access = protected)
function respondToXYZChange(obj, value) % no function definition here
end
end
end
classdef MySubClass < MySuperClass
methods
function obj=MySubClass()
disp('!');
end
function update(obj)
disp('updating...');
end
end
methods(Access = protected)
function respondToXYZChange(obj) % call to the "update" method of subclass
obj.update();
end
end
end
>> m = MySubClass3;
!
>> m.xyz = 'a'
setting xyz in MySuperClass to a
updating...
m =
MySubClass3 with properties:
xyz: 'a'
>>
Executing the command:
m.xyz = 'a';
calls the superclass "set" method which sets the value of the property "xyz" and then calls the protected "respondToXYZChange" which is defined in the subclass to call the "update" method.

추가 답변 (1개)

Robert
Robert 2021년 8월 27일
How about this?
classdef MySuperClass < handle
properties
xyz
end
methods
function set.xyz(obj,value)
disp(['setting xyz in MySuperClass to ', value]);
obj.xyz = value; % updates the property
if class(obj) == "MySubClass"
respondToXYZChange(obj) % calls the subclass method
end
end
end
end
classdef MySubClass < MySuperClass
methods
function respondToXYZChange(obj)
disp("updating...")
end
end
end
This way, there is no need to define respondToXYZChange in the superclass.
  댓글 수: 2
Steven Lord
Steven Lord 2021년 8월 27일
But this approach means that the superclass needs to be modified any time a new subclass gets defined, to add it to the list of classes that set.xyz needs to know about. This is a violation of the open-closed principle.
Robert
Robert 2021년 8월 27일
That's definitely something to take into account, yes!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by