MATLAB Answers

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

조회 수: 2(최근 30일)
Oliver Grossmann
Oliver Grossmann 2021년 6월 7일
편집: Matt J 2021년 6월 8일
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!

채택된 답변

Matt J
Matt J 2021년 6월 7일
편집: Matt J 2021년 6월 8일
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.
  댓글 수: 1
Oliver Grossmann
Oliver Grossmann 2021년 6월 8일
I guess this is the most sensible answer for my case, thank you for this answer.

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

추가 답변(1개)

Jeff Miller
Jeff Miller 2021년 6월 7일
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))

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by