How to overload class functions for custom functions

조회 수: 8 (최근 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 CenterFile Exchange에서 Sample Class Implementations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by