Programming Previous page   Next Page

The set and get Methods

The set and get methods provide a convenient way to access object data in certain cases. For example, suppose you have created a class that defines an arrow object that MATLAB can display on graphs (perhaps composed of existing MATLAB line and patch objects).

To produce a consistent interface, you could define set and get methods that operate on arrow objects the way the MATLAB set and get functions operate on built-in graphics objects. The set and get verbs convey what operations they perform, but insulate the user from the internals of the object.

Examples of set and get Methods

See the following sections for examples of set and get methods:

Property Name Methods

As an alternative to a general set method, you can write a method to handle the assignment of an individual property. The method should have the same name as the property name.

For example, if you defined a class that creates objects representing employee data, you might have a field in an employee object called salary. You could then define a method called salary.m that takes an employee object and a value as input arguments and returns the object with the specified value set.


Previous page  Accessing Object Data Indexed Reference Using subsref and subsasgn Next page

© 1994-2005 The MathWorks, Inc.