How to overload class functions for custom functions

5 ビュー (過去 30 日間)
Nathan Jessurun
Nathan Jessurun 2018 年 7 月 31 日
コメント済み: Sean de Wolski 2018 年 8 月 7 日
Built-in matlab classes allow you to specify your own function overloads by mimicking directory structure:
% Overloads 'disp' method:
@char/disp.m
How can I duplicate this behavior for my custom classes? I.e. if I have a class structure:
+MyPackage
@MyClass
myMethod.m
anotherMethod.m % I want this to stay the same in both cases
MyClass.m
How can I overload this elsewhere to have
+DifferentPackage
@MyClass
myMethod.m % alternative myMethod
Sorry if the question is poorly phrased -- I'll do my best to clarify / edit as needed

回答 (1 件)

Steven Lord
Steven Lord 2018 年 7 月 31 日
Can you do this? I'm not certain. I know in previous releases having multiple class directories on the path would treat the union of the files in those directories as being the definition of the class, but that has changed. From the documentation: "In MATLAB Versions 5 through 7, class folders do not shadow other class folders having the same name, but residing in later path folders. Instead, the class uses the combination of methods from all class folders having the same name to define the class. This behavior is no longer supported."
Should you do this? I would not, as I think it would be likely that you would try to run / debug one of the versions of the method but actually be calling the other.
If you describe in more detail the application you're trying to implement using this approach we may be able to offer a different suggestion, one that avoids deliberate shadowing.
  4 件のコメント
Steven Lord
Steven Lord 2018 年 8 月 7 日
I'm on vacation so your best bet is probably to contact Technical Support using the Contact Us link in the upper-right corner of this page and explain in more detail why you're trying to do this (not just what you're trying to do; there may be a different design that will achieve your goal without the complexity of your current design) and ask them for suggestions.
Sean de Wolski
Sean de Wolski 2018 年 8 月 7 日
If they are two variations, then I'd make them both inherit from the same base class with different subclass implementations of MyMethod.

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

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by