Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB - list all methods supplied by subclass only?

Tags:

oop

matlab

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.

Thanks!

like image 271
Dang Khoa Avatar asked Jun 14 '11 20:06

Dang Khoa


People also ask

How can I see class methods in MATLAB?

methodsview( packagename. classname ) displays information about the methods in the class classname . If the class is in a package, include packagename . If classname is a MATLAB® or Java® class, methodsview lists only public methods, including those methods inherited from superclasses.

How do you call a method of super class from sub class?

Subclass methods can call superclass methods if both methods have the same name. From the subclass, reference the method name and superclass name with the @ symbol.

How do you call a class method in MATLAB?

To call a static method, prefix the method name with the class name so that MATLAB can determine what class defines the method. Call staticMethod using the syntax classname . methodname : r = MyClass.

Can a class inherit from multiple classes MATLAB?

When you create a subclass derived from multiple superclasses, the subclass inherits the properties, methods, and events defined by all specified superclasses. If more than one superclass defines a property, method, or event having the same name, there must be an unambiguous resolution to the multiple definitions.


1 Answers

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'
like image 185
gnovice Avatar answered Sep 25 '22 23:09

gnovice