| MATLAB® | ![]() |
| On this page… |
|---|
Establishing an object precedence enables the MATLAB® runtime 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 left-most object is called. However, there are two exceptions:
User-defined classes have precedence over MATLAB built-in classes.
User-defined classes can specify their relative precedence with respect to other user-defined classes using the InferiorClasses attribute.
In Example — A Polynomial Class, the polynom 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
calls the DocPolynom plus method (which converts the double, 1, to a DocPolynom object and then implements the addition of two polynomials). The user-defined DocPolynom class has precedence over the built–in double class.
You can specify the relative precedence of user-defined classes by listing inferior classes using a class attribute. The InferiorClasses property places a class below other classes in the precedence hierarchy. Define the InferiorClasses property in the classdef statement:
classdef (inferiorClasses = {?class1,?class2}) myClass
This attribute establishes a relative priority of the class being defined with the order of the classes listed.
If objectA is above objectB in the precedence hierarchy, then the expression
objectA + objectB
calls @classA/plus.m. Conversely, if objectB is above objectA in the precedence hierarchy, then the MATLAB runtime calls @classB/plus.m.
See Rules for Naming to Avoid Conflicts for related information.
![]() | Implementing Operators for Your Class | Class Methods for Graphics Callbacks | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |