How to get the name of the calling class in a static method?

Hi,
is there a way so that a static method can get the name of the class that is calling it?
In a non-static method, one always has the option of running class(obj) obj being the object on whihc the method is being applied. But how to do it for a static method?

 Akzeptierte Antwort

Guillaume
Guillaume am 6 Okt. 2014
mfilename('class')
However, since inheritance doesn't really have any effect on static methods, you could just hardcode it in the method.

5 Kommentare

Image Analyst
Image Analyst am 6 Okt. 2014
Bearbeitet: Image Analyst am 6 Okt. 2014
Daniel's "Answer" moved here since it's not an "Answer" to the original posting, and so he can properly accept Guillaume's answer instead of his own.
Thanks Guillaume!
In any case I'm curious about your comment on inheritante not having any effect on static methods.
I have a hierarchy of subclasses. The static method "showInfo" is common to all of them, but each class has a different (static) 'extractInfo' method, used by 'showInfo'. So, I need my 'showInfo' method to be able to detect which class is calling it, so that it can use the appropriate 'extractInfo' method of the calling class.
I don't really see how I could hardcode this, but in any case I'm tring now the mfilename trick.
Thanks, IA.
Daniel, I'm afraid you can't do it like that. Your showinfo method, if not overridden by derived class, will always be called from the base class, so you won't be able to dispatch to the derived class.
You'll have to do it the other way round, have a static showinfo that's overridden in all derived classes and that calls another private static method in the base class for the common code.
hm... thanks,Guillaume, I see... tested it, and it's like you said: showinfo is called from the base class, and there is no trivial way (known to me) to track back if it was invoked form a subclass.
Well, the option of overriding showinfo is exactly what I didn't want to do, as there is no reason from each subclass to contain an exact copy of the same function. I'll try with mbstack or something like that, to see if I can trace back the initiating subclass...
Well, the simplest thing to do what you want is to take the showinfo method out of the class definition and implement it as a free function, on its own:
function showinfo(classname)
if ismember('baseclassname', superclasses(classname))
info = eval(sprintf('%s.extractinfo')); %dispatch to static method
%common code for showinfo
else
error('showinfo only applies to classes derived from baseclassname');
end
end
The calling syntax does not differ much anyway:
%using hypotetical derived static method that would know where it's called from:
derivedclassname.showinfo;
%using free function:
showinfo('derivedclassname');
Yes, it works, so I'll have to do it like this.
The calling syntax was actually important, as the idea is that the users of my package know the available class names and can find out through autocompletion the available methods accessible to the users. That's the standard setting in my system, but well. Sometimes one cannot avoid exceptions.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Another option is you can pass an empty version of the subclass enumeration to the superclass method. This would mean you have to create a version of the function in each subclass, which I know is a bit of a pain, but it ends up being a very simple, one line function in the subclass. Below is a very simple example, but this works really well if the static function you want to use in the superclass it is in rather complicated as you can use a simple pass through to call it from the subclass.
classdef mySuperClass
methods(static)
function name = getName ( subObj )
arguments
subObj mySuperClass
end
name = class(subObj);
%Note: you can call any other static functions or properties using myEnum.staticname
end
end
end
classdef mySubClassA
methods(static)
function name = getName()
name = getName@mySuperClass( mySubClassA.empty );
end
end
end
classdef mySubClassB
methods(static)
function name = getName()
name = getName@mySuperClass( mySubClassB.empty );
end
end
end
now mySubClassA.getName will return "mySubClassA" and likewise for mySubClassB
If you do similar with other static functions - by passing an empty class in to subObj, you can call the getName function using subObj.getName.

Kategorien

Mehr zu Data Type Identification finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by