Calling a class's static method from its metadata

4 ビュー (過去 30 日間)
Thomas Atkinson
Thomas Atkinson 2020 年 3 月 12 日
編集済み: Matt J 2020 年 3 月 12 日
I am looking to create a number of classes which all subclass a superclass, AnalysisFunction in this case, which defines one abstract static method, results = apply(ClassWithData).
I want a controller class to be able to call apply(classWithData) on all classes which subclass AnalysisFunction. My initial plan is to create a namespace to house all the subclasses I want to be called then in the controller get the namespace's metadata object, loop through the present classes, check if each subclasses AnalysisFunction and if it does, call apply(classWithData) on the class.
To do this, I need to be able to evaluate a method against a class from its metadata. A simple, though in my opinion ugly, way of doing this is to use eval with the class's name (from the metadata object) and .apply(classWithData) concatenated as a string.
Is there a way to avoid the use of eval, or indeed a better way entirely from what I am proposing to achieve the calling of such an abstract static function on all subclasses? Thanks in advance!
  3 件のコメント
Thomas Atkinson
Thomas Atkinson 2020 年 3 月 12 日
Ah, sorry, probably wasn't so clear. classWithData is another class I've made such that it behaves like a struct (SetAccess = private) so I can store data centrally and have each analysis function fetch what it needs.
Thomas Atkinson
Thomas Atkinson 2020 年 3 月 12 日
Here's a rubbish UML of what I'm trying to get. classWithData is an instance of DataStore.

サインインしてコメントする。

回答 (1 件)

Matt J
Matt J 2020 年 3 月 12 日
編集済み: Matt J 2020 年 3 月 12 日
or indeed a better way entirely from what I am proposing to achieve the calling of such an abstract static function on all subclasses?
If you have an object of the sub-class present, you can use it to call the Static method of the sub-class.
if isa(obj,'AnalysisFunction')
obj.apply(classWithData);
end
  1 件のコメント
Matt J
Matt J 2020 年 3 月 12 日
編集済み: Matt J 2020 年 3 月 12 日
Is there a way to avoid the use of eval
Aside from the above, you could use feval instead.
feval(str2func(class(obj)+".apply"), classWithData)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by