How to set a protected superclass property from subclass method?
6 次查看(过去 30 天)
显示 更早的评论
I have an abstract superclass with some protected properties. I have a subclass with a set method that takes name-value pairs of the superclass and subclass and assigns them. However, when i call the set method from an instance of the subclass, I cannot set the superclass properties as Matlab gives an error saying that the name-values must be properties of the subclass.
For example:
m = MVA();
m.set('Events',struct('A',1,'B',2)); % works fine since Events is a property of MVA
m.set('analysisCycle','Test'); % does not work and errors saying that analysisCyle must be a property of MVA. this is even though analysisCycle is a protected property
Setting the analysisCycle proeprty from an instance of MVA works when the superclass property is public. However, I'd like to not have it public.
How can I set a superclass protected property from an instance of a subclass? The issue might have to do with how property validate the inputs of my set method in the sublass (eg, propertyArguments.?MVA). However, I beleive this should still give access to protected superclass properties.
0 个评论
回答(1 个)
Matt J
2025-9-11
编辑:Matt J
2025-9-11
You cannot use the ?.MVA syntax on non-public properties. Since you already have validation in the base class, I don't think you need it in your set() method or MVA() constructor as well. I would just do,
function obj = set(obj,varargin)
%SET Set property value.
% Syntax:
% obj.set(Name,Value)
propertyArguments=struct(varargin{:});
fieldNames = fieldnames(propertyArguments);
for i = 1:numel(fieldNames)
propName = fieldNames{i};
obj.(propName) = propertyArguments.(propName);
end
obj.modifiedBy = getUser();
obj.modifiedDate = datetime("now");
end
4 个评论
Matt J
2025-9-11
编辑:Matt J
2025-9-11
This would only check for DataClass properties, right? Wouldn't it error if you had an MVA property?
Yes, but it's an easy modification,
mc=?MVA; validprops ={mc.PropertyList.Name};
Now, validprops will contain all MVA properties, including those inherited from DataClass.
Wouldn't you need to do something else to error check for MVA properties? And then some kind of OR logic to discern between DataClass and MVA properties?
So, no. In light of the above, I don't see a real need for that. If for some reason, you needed to distinguish between properties based on whether or not they were inherited from DataClass, you could construct a table or dictionary giving the DefiningClass of each property, e.g.,
mc=?MVA;
pl =mc.PropertyList;
T=table(string({pl.Name}'), arrayfun(@(z)string( z.Name),[pl.DefiningClass]'))
T =
8×2 table
Var1 Var2
_______________ ___________
"data" "MVA"
"Events" "MVA"
"analysisCycle" "DataClass"
"dataLog" "DataClass"
"modifiedBy" "DataClass"
"modifiedDate" "DataClass"
"createdBy" "DataClass"
"createdDate" "DataClass"
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Power Transmission and Distribution 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!