| MATLAB® | ![]() |
| On this page… |
|---|
Meta-classes are classes that contain information about class definitions. Each block in a class definition has an associated meta-class that defines the attributes for that block. Each attribute corresponds to a property in the meta-class. An instance of a meta-class has values assigned to each property that correspond to the values of the attributes of the associated class block.
Meta-classes enable introspection of class definitions, which is useful for programmatic inspection of classes and objects. Such techniques are used by tools such as property inspectors, debuggers, and so on.
The meta package is a package of meta-classes that are involved in the definition of classes and class components. The class name indicates the component described by the meta-class:
meta.package meta.class meta.property meta.method meta.event
Each meta-class has properties, methods, and events that contain information about the class or class component. See meta.package, meta.class, meta.property, meta.method and meta.event for more information on these meta-classes.
You can create meta-class objects from class instances or from the class definition directly.
? operator — Returns a meta-class object for the named class
metaclass — Returns a meta-class object for the class instance
mobj = ?classname; % create meta-class object from class name obj = myClass; mobj = metaclass(obj); % create meta-class object from class instance
Note that the metaclass function returns the meta.class object (i.e., an object of the meta.class class). You can obtain other meta-class objects (meta.property, meta.method, etc.) from the meta.class object.
Note Meta-class is a term used to describe a kind of class. meta.class is a class in the meta package whose instances contain information about MATLAB® classes. |
Here is how you can use meta-class objects:
Obtain a meta.class object from a class definition (using ?) or from a class instance (using metaclass).
Use the meta.class properties, methods, and events to obtain information about the class or class instance from which you obtained the meta.class object, including getting other meta-class objects, such as the meta.properties objects defined for each of the class's properties.
The following examples show these techniques.
Consider the following class and the information you can obtain from its meta-class instances. The EmployeeData class is a handle class with two properties, one of which defines a set access function. The capability to glean information about the class programmatically without requiring an instance of the class can be useful when implementing tools, such as inspectors, code debuggers, and so on.
classdef EmployeeData < handle properties EmployeeName end properties (GetAccess = private) EmployeeNumber end methods function obj = EmployeeData(name,ss) obj.EmployeeName = name; obj.EmployeeNumber = ss; end function obj = set.EmployeeName(obj,name) if ischar(name) obj.EmployeeName = name; else error('Employee name must be a text string') end end end end
Using the EmployeeData class defined above, you can create a meta.class object using the ? operator:
mboj = ?EmployeeData;
You can determine what classes EmployeeData is derived from:
a = mobj.SuperClasses; % a is cell array of meta.class objects
a{1}.Name
ans =
handleInspecting Properties. Suppose you want to know the names of the properties defined by this class. First obtain a cell array of meta.properties objects from the meta.class Properties property.
mpCell = mobj.Properties;
The length of mpCell indicates there are two meta.property objects, one for each property:
length(mpCell)
ans =
2Now get a meta.property object from the cell array:
prop1 = mpCell{1}
prop1 =
meta.property % prop1 is a meta.property object
prop1.Name
ans =
EmployeeName % first object is EmployName property's meta.propertyYou can now query any of the meta.property object's properties for the EmployName class. You can determine the setting of all property attributes and even obtain a function handle to the property's set access function:
setmeth = prop1.SetMethod setmeth = @D:\MyDir\@EmployeeData\EmployeeData.m>EmployeeData.set.EmployeeName
Querying the meta.property class SetMethod property returns a function handle to the set access method defined in the EmployeeData class.
Suppose you create a EmployeeData object:
EdObj = EmployeeData('My Name',1234567); mEdObj = metaclass(EdObj); mpCell = mEdobj.Properties; eval(['EdObj.',mpCell{1}.Name]) ans = My Name eval(['EdObj.',mpCell{2}.Name]) ??? Getting the 'EmployeeNumber' property of the 'EmployeeData' class is not allowed. mpCell{2}.GetAccess ans = private
![]() | Defining Named Constants | Value or Handle Class — Which to Use | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |