OOP: Reference (subclass) enumeration names from static superclass method. Possible?

2 views (last 30 days)
Oliver Grossmann
Oliver Grossmann on 7 Jun 2021
Edited: Matt J on 8 Jun 2021
Hi, I'm stuck on a MATLAB OOP problem.
I have several enumeration classes that are subclasses to (the abstract) MySuperClass. For Example:
classdef MyEnum1 < MySuperClass
methods(Static)
function result = doWork()
allNames = string(enumeration('MyEnum1'));
result = foo(allNames);
end
end
enumeration
A
B
C
end
end
classdef MyEnum2 < MySuperClass
methods(Static)
function result = doWork()
allNames = string(enumeration('MyEnum2'));
result = foo(allNames);
end
end
enumeration
D
E
F
end
end
% and so on...
Then there is one function which all enumeration classes have in common, that does some work on the enumeration members names, in this case it's the static method doWork(), which is shown above.
I feel like it should be possible to define this method in the (abstract) superclass, since it does the same thing every time and only operates on a different list of enumeration member names, which is dependent on the subclass calling the function. But I can't figure out how to dynamically reference the subclass that is calling the static method.
The following solution seems to work, but I'm giving up the (Static) of my method. Ideally, I would prefer a solution in which I can doWork without having to create an instance of the respective subclass/enumeration class.
classdef(Abstract) MySuperClass
methods
function result = doWork(obj)
allNames = string(enumeration(obj)); % could work, but is not preferred
result = foo(allNames);
end
end
end
What I really want is something like this:
classdef(Abstract) MySuperClass
methods (Static)
function result = doWork
allNames = string(enumeration(?????)); % this?/self?/metaclass?
result = foo(allNames);
end
end
end
so that I may finally use the static method of the superclass as follows:
MyEnum1.doWork
MyEnum2.doWork
%... and so on
Without having to copypaste identical code into each enumeration's classdef.
Thank you so much in advance for your insights!

Accepted Answer

Matt J
Matt J on 7 Jun 2021
Edited: Matt J on 8 Jun 2021
No, you can't unfortunately, but you can avoid copying the method to all the sub-classdef's just by making doWork() an external method (i.e., not a method of a class):
function result = doWork(classname)
allNames = string(enumeration(classname)); % this?/self?/metaclass?
result = foo(allNames);
end
You might consider putting all your classdefs in a common folder, and the doWork.m file in a private/ subfolder., so that it will be visible only to this family of classes.

More Answers (1)

Jeff Miller
Jeff Miller on 7 Jun 2021
If doWork really just needs the names, maybe pass allnames as an argument of doWork, something like this:
function result = doWork(allNames)
result = foo(allNames);
end
% and use it like
doWork(enumeration(MyEnum1))
% or
MySuperClass.doWork(enumeration(MyEnum1))

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!