开发者

MATLAB - list all methods supplied by subclass only?

I have a class that inherits from multiple superclasses, and I would like to get the methods that the class has. Naively using methods() returns methods from the class I'm working with as well as superclass methods, but I'm not interested in the superclass methods.

Any idea how to do this? I couldn't find anything in MATLAB documentation.

开发者_如何学Go

Thanks!


If your subclass doesn't reimplement any of the methods of the superclasses (or if you're fine with ignoring reimplemented methods), you can use the functions METHODS and SUPERCLASSES to find a list of subclass methods that aren't also methods of any of the superclasses. For example:

>> obj = 'hgsetget';  %# A sample class name
>> supClasses = superclasses(obj)

supClasses = 

    'handle'    %# Just one superclass, but what follows should handle more

>> supMethods = cellfun(@methods,supClasses,...  %# Find methods of superclasses
                        'UniformOutput',false);
>> supMethods = unique(vertcat(supMethods{:}));  %# Get a unique list of
                                                 %#   superclass methods
>> subMethods = setdiff(methods(obj),supMethods)  %# Find methods unique to the
                                                  %#   subclass
subMethods = 

    'get'
    'getdisp'
    'set'
    'setdisp'


Even though this question is solved, let me add another answer using meta.class capabilities:

%# some class name
clname = 'hgsetget';

%# obtain class meta-info
mt = meta.class.fromName(clname);

%# get name of class defining each method
cdef = arrayfun(@(c)c.Name, [mt.MethodList.DefiningClass], 'Uniform',false);

%# keep only methods that are defined in the subclass
subMethods = {mt.MethodList(ismember(cdef,clname)).Name}

The result for this example:

subMethods = 
    'set'    'get'    'setdisp'    'getdisp'    'empty'

Note how the result also includes the static methods empty which all non-abstract classes have (used to create an empty array of that class).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜