MATLAB Object-Oriented Programming

(Joyce) #1
A.B = C

Introduce a new variable, A, that is a struct containing a field B whose value is C. If A.B
= C could refer to a static property of class A, then class A would take precedence over
variable A.

This behavior would be a significant incompatibility with prior releases of MATLAB. For
example, the introduction of a class named A on the MATLAB path could change the
meaning of an assignment statement like A.B = C inside a .m code file.

In other languages, classes rarely use static data, except as private data within the class
or as public constants. In MATLAB, you can use constant properties the same way you use
public final static fields in Java. To use data that is internal to a class in MATLAB,
create persistent variables in private or protected methods or local functions used
privately by the class.

Avoid static data in MATLAB. If a class has static data, using the same class in multiple
applications causes conflicts among applications. Conflicts are less of an issue in some
other languages. These languages compile applications into executables that run in
different processes. Each process has its own copy of the class static data. MATLAB,
frequently runs many different applications in the same process and environment with a
single copy of each class.

For ways to define and use static data in MATLAB, see “Static Data” on page 4-2.

Common Object-Oriented Techniques


This table provides links to sections that discuss object-oriented techniques commonly
used by other object-oriented languages.

Technique How to Use in MATLAB
Operator overloading “Operator Overloading” on page 17-47
Multiple inheritance “Subclassing Multiple Classes” on page 12-22
Subclassing “Design Subclass Constructors” on page 12-9
Destructor “Handle Class Destructor” on page 7-16
Data member scoping “Property Attributes” on page 8-9
Packages (scoping
classes)

“Packages Create Namespaces” on page 6-25

5 Class Definition—Syntax Reference

Free download pdf