How to overload class functions for custom functions

6 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Software Development Tools 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by