MATLAB Object-Oriented Programming

(Joyce) #1

Call Methods


Call the roundOff method on object a:

roundOff(a)

ans =

1.0500

Pass the object as the first argument to a method that takes multiple arguments:

multiplyBy(a,3)

ans =

3.1416

You can also call a method using dot notation:

a.multiplyBy(3)

It is not necessary to pass the object explicitly as an argument when using dot notation.
The notation uses the object to the left of the method name.

For information on class methods, see “Methods and Functions” on page 5-15

Add Constructor


Classes can define a special method to create objects, called a constructor. Constructor
methods enable you to pass arguments to the constructor, and to validate and assign
property values. Here is a constructor for the BasicClass class:

methods
function obj = BasicClass(val)
if nargin > 0
if isnumeric(val)
obj.Value = val;
else
error('Value must be numeric')
end
end
end
end

2 Basic Example

Free download pdf