MATLAB Object-Oriented Programming

(Joyce) #1
p = Pump;
p.Speed = 'm'

p =

Pump with properties:

Speed: Medium

The numerical value is available from the property.

getGPM(p)

Flow rate is: 50 GPM

For information about enumeration classes, see “Define Enumeration Classes” on page
14-5.

Integer Class Validation

MATLAB supports several integer classes (see “Integers”). However, restricting a
property to an integer class can result in integer overflow. The resulting value can
saturate at the maximum or minimum value in the integer’s range.

When integer overflow occurs, the value that is assigned to a property might not be the
value from the right side of the assignment statement.

For example, suppose that you want to restrict a property value to a scalar uint8.

classdef IntProperty
properties
Value(1,1) uint8
end
end

Assigning a numeric value to the Value property effectively casts the numeric value to
uint8, but does not result in an error for out-of-range values.

a = IntProperty;
a.Value = -10;
disp(a.Value)

0

8 Properties — Storing Class Data

Free download pdf