23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Classes and Objects: An Overview<br />

The syntax for invoking a method on an object is similar to a function call.<br />

Generally, it looks like<br />

[out1,out2,...] = methodName(object,arg1,arg2, ...);<br />

For example, suppose a user-defined class called polynom has a char method<br />

defined for the class. This method converts a polynom object to a character<br />

string and returns the string. This statement calls the char method on the<br />

polynom object p.<br />

s = char(p);<br />

Using the class function, you can confirm that the returned value s is a<br />

character string.<br />

class(s)<br />

ans =<br />

char<br />

s<br />

s =<br />

x^3-2*x-5<br />

You can use the methods command to produce a list of all of the methods<br />

that are defined for a class.<br />

Private Methods<br />

Private methods can be called only by other methods of their class. You define<br />

private methods by placing the associated M-files in a private subdirectory<br />

of the @classname directory. In the example,<br />

@classname/private/updateObj.m<br />

the method updateObj has scope only within the classname class. This<br />

means that updateObj can be called by any method that is defined in the<br />

@classname directory, but it cannot be called from the <strong>MATLAB</strong> command<br />

line or by methods outside of the class directory, including parent methods.<br />

Private methods and private functions differ in that private methods (in fact<br />

all methods) have an object as one of their input arguments and private<br />

9-5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!