Object Converters
Why Implement Converters
You can convert an object of one class to an object of another class. A converter method has the same name as the class it converts to, such aschar
ordouble
. Think of a converter method as an overloaded constructor method of another class. The converter takes an instance of its own class and returns an object of a different class.
Converters enable you to:
Use methods defined for another class
Ensure that expressions involving objects of mixed class types execute properly
Control how instances are interpreted in other contexts
Suppose that you define apolynomial
class. If you create adouble
method for thepolynomial
class, you can use it to call other functions that require inputs of typedouble
.
p = polynomial(...);dp = double(p); roots(dp)
p
is a polynomial object,double
is a method of thepolynomial
class, androots
is a standard MATLAB®function whose input arguments are the coefficients of a polynomial.
Converters for Package Classes
Classes defined in packages can have names that are a dot-separated list of names. The last name is a class and preceding names are packages. Name the conversion methods using the package qualifiers in the method names. For example, a conversion method to convert objects ofMyClass
to objects of thePkgName.PkgClass
class uses this method name:
classdefMyClass...methodsfunctionobjPkgClass = PkgName.PkgClass(objMyclass)...endendend
You cannot define a converter method that uses dots in the name in a separate file. Define package-class converters in theclassdef
file.
Converters and Subscripted Assignment
When you make a subscripted assignment statement like:
A(1) = myobj;
MATLAB比较右variab的类le to the class of the Left-Side variable. If the classes are different, MATLAB attempts to convert the Right-Side variable to the class of the Left-Side variable. To do this conversion, MATLAB first searches for a method of the Right-Side class that has the same name as the Left-Side class. Such a method is a converter method, which is similar to a typecast operation in other languages.
If the Right-Side class does not define a method to convert from the Right-Side class to the Left-Side class, MATLAB calls the Left-Side class constructor. passing it the Right-Side variable.
For example, suppose that you make the following assignments:
A(1) = objA;% Object of class ClassAA(2) = objB;% Object of class ClassB
MATLAB attempts to call a method ofClassB
namedClassA
. If no such converter method exists, MATLAB software calls theClassA
constructor, passingobjB
as an argument. If theClassA
constructor cannot acceptobjB
as an argument, then MATLAB returns an error.
Usecell
数组来存储啊bjects of different classes.
Converter for Heterogeneous Arrays
To support the formation of heterogeneous arrays using objects that are not part of the heterogeneous hierarchy, implement aconvertObject
method in the root superclass. TheconvertObject
method must convert the nonmember object to a valid member of the heterogeneous hierarchy.
For details on implementing theconvertObject
method, seematlab.mixin.Heterogeneous
.