How can I call the set-dot method on a subclass object to set a property of the superclass?
4 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2018 年 3 月 9 日
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
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.
0 件のコメント
その他の回答 (1 件)
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
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.
参考
カテゴリ
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!