Programming Previous page   Next Page

Creating Objects

You create an object by calling the class constructor and passing it the appropriate input arguments. In MATLAB, constructors have the same name as the class name. For example, the statement,

creates an object named p belonging to the class polynom. Once you have created a polynom object, you can operate on the object using methods that are defined for the polynom class. See Example -- A Polynomial Class for a description of the polynom class.

Invoking Methods on Objects

Class methods are M-file functions that take an object as one of the input arguments. The methods for a specific class must be placed in the class directory for that class (the @classname directory). This is the first place that MATLAB looks to find a class method.

The syntax for invoking a method on an object is similar to a function call. Generally, it looks like

For example, suppose a user-defined class called polynom has a char method defined for the class. This method converts a polynom object to a character string and returns the string. This statement calls the char method on the polynom object p.

Using the class function, you can confirm that the returned value s is a character string.

You can use the methods command to produce a list of all of the methods that are defined for a class.


Previous page  MATLAB Data Class Hierarchy Private Methods Next page

© 1994-2005 The MathWorks, Inc.