How to overload class functions for custom functions

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 个)

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 个评论

Thanks for the reply.
Working off the classes shown above, I have a case where classes in +DifferentPackage use @MyClass, but a very small difference is needed in myMethod.
I figured I have these options:
1) Make all @MyClass private members protected, and overload myMethod in the inherited class. I didn't want to do this since MyClass follows conventions of other classes in +MyPackage who have private properties and methods. These classes need no modifications to work in +DifferentPackage.
2) Copy-paste the class and change the desired function (Thinking about this makes me shudder, as it basically spits in the face of maintainability)
3) The question posed above, where I can redefine that one method. I thought this most clearly showed the idea that it is not a subclass, but rather a minor variation of the other class. I can see where what you said causes problems, though
4) Rewrite myMethod to make it more general in @MyClass. This would take a bit too much time, and add misdirection in both cases it's used
In a traditional language (e.g. C++) I think I would make myMehod a friend and differentiate using namespaces
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.
If they are two variations, then I'd make them both inherit from the same base class with different subclass implementations of MyMethod.

请先登录,再进行评论。

类别

Community Treasure Hunt

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

Start Hunting!

Translated by