MATLAB Object-Oriented Programming

(Joyce) #1

% Function that returns a value
Prop3 = datestr(now)


Class Constructor Methods............................ 9-


Prop4 = AccountManager
end
methods (Static)
function accNum = setupAccount
accNum = randi(9,[1,12]);
end
end
end


MATLAB does not call property set methods when assigning the result of default value
expressions to properties. (See “Property Access Methods” on page 8-50 for information
about these special methods.)


Enumerations that derived from MATLAB types can use expression to assign a value:


classdef FlowRate < int32
enumeration
Low (10)
Medium (FlowRate.Low5)
High (FlowRate.Low
10)
end
end


MATLAB evaluates these expressions only once when enumeration members are first
accessed.


Expressions in Attribute Specifications


For attributes values that are logical true or false, class definitions can specify
attribute values using expressions. For example, this assignment makes MyClass sealed
(cannot be subclassed) for versions of MATLAB before R2014b (verLessThan)


classdef (Sealed = verLessThan('matlab','8.4')) MyClass


The expression on the right side of the equal sign (=) must evaluate to true or false.
You cannot use any definitions from the class file in this expression, including any
constant properties, static methods, and local functions.


While you can use conditional expression to set attribute values, doing so can cause the
class definition to change based on external conditions. Ensure that this behavior is
consistent with your class design.


Evaluation of Expressions in Class Definitions 6-

Free download pdf