MATLAB Object-Oriented Programming

(Joyce) #1
y = rand(1,10);
md = MyData(y);

Call the method using the object:

bar(md)

You can also use dot notation:

md.bar

Implementing MATLAB Operators

Classes designed to implement new MATLAB data types typically define certain
operators, such as addition, subtraction, or equality.

For example, standard MATLAB addition (+) cannot add two polynomials because this
operation is not defined by simple addition. However, a polynomial class can define its
own plus method that the MATLAB language calls to perform addition of polynomial
objects when you use the + symbol:

p1 + p2

For information on overloading operators, see “Operator Overloading” on page 17-47.

Rules for Naming to Avoid Conflicts


The names of methods, properties, and events are scoped to the class. Therefore, adhere
to the following rules to avoid naming conflicts:


  • You can reuse names that you have used in unrelated classes.

  • You can reuse names in subclasses if the member does not have public or protected
    access. These names then refer to entirely different methods, properties, and events
    without affecting the superclass definitions

  • Within a class, all names exist in the same name space and must be unique. A class
    cannot define two methods with the same name and a class cannot define a local
    function with the same name as a method.

  • The name of a static method is considered without its class prefix. Thus, a static
    method name without its class prefix cannot match the name of any other method.


9 Methods — Defining Class Operations

Free download pdf