MATLAB Object-Oriented Programming

(Joyce) #1

Object Precedence in Method Invocation


In this section...
“Object Precedence” on page 9-47
“Defining Precedence” on page 9-48

Object Precedence


Establishing an object precedence enables MATLAB to determine which of possibly many
versions of an operator or function to call in a given situation.

For example, consider the expression

objectA + objectB

Ordinarily, objects have equal precedence and the method associated with the leftmost
object is called. However, there are two exceptions:


  • Classes defined with the classdef syntax have precedence over these MATLAB
    classes:


double, single, int64, uint64, int32, uint32, int16, uint16, int8, uint8,
char, string, logical, cell, struct, and function_handle.


  • Classes defined with the classdef syntax can specify their relative precedence with
    respect to other classes using the InferiorClasses attribute.


Consider the example in “Representing Polynomials with Classes” on page 19-2. The
DocPolynom class defines a plus method that enables the addition of DocPolynom
objects. Given the object p:

p = DocPolynom([1 0 -2 -5])
p =
x^3-2*x-5

the expression:

1 + p
ans =
x^3-2*x-4

Object Precedence in Method Invocation
Free download pdf