Main Content

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 ascharordouble. 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 apolynomialclass. If you create adoublemethod for thepolynomialclass, you can use it to call other functions that require inputs of typedouble.

p = polynomial(...);dp = double(p); roots(dp)

pis a polynomial object,doubleis a method of thepolynomialclass, androotsis 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 ofMyClassto objects of thePkgName.PkgClassclass 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 theclassdeffile.

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 ofClassBnamedClassA. If no such converter method exists, MATLAB software calls theClassAconstructor, passingobjBas an argument. If theClassAconstructor cannot acceptobjBas 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 aconvertObjectmethod in the root superclass. TheconvertObjectmethod must convert the nonmember object to a valid member of the heterogeneous hierarchy.

For details on implementing theconvertObjectmethod, seematlab.mixin.Heterogeneous.

Related Topics