How to set a protected superclass property from subclass method?

6 visualizaciones (últimos 30 días)
Chris Nemecek
Chris Nemecek el 11 de Sept. de 2025
Editada: Matt J el 11 de Sept. de 2025
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.

Respuestas (1)

Matt J
Matt J el 11 de Sept. de 2025
Editada: Matt J el 11 de Sept. de 2025
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 comentarios
Chris Nemecek
Chris Nemecek el 11 de Sept. de 2025
This would only check for DataClass properties, right? Wouldn't it error if you had an MVA property?
mc=?DataClass;
validprops ={mc.PropertyList.Name};
assert( all(ismember(fieldNames, validprops)),...
'Invalid name-value pair');
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?
Matt J
Matt J el 11 de Sept. de 2025
Editada: Matt J el 11 de Sept. de 2025
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"

Iniciar sesión para comentar.

Categorías

Más información sobre Power Transmission and Distribution en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by