阵列创建功能万博1manbetx的集体支持
Extend Array-Creation Functions for Your Class
有几个matlab®创建特定尺寸和类型的数组的功能,例如那些
和zeros
。用户定义的类可以添加对数组创建功能的支持,而无需使用超载方万博1manbetx法语法。
Class support for any of the array-creation functions enables you to develop code that you can share with built-in and user-defined data types. For example, the class of the variablex
在以下代码中,可以是初始开发过程中的内置类型,然后被用户定义的类取代,该类透明地超载zeros
:
cls = class(x);zarray =零(m,n,cls);
数组创建函数以两种方式创建特定类型的数组:
Class name syntax — Specify class name that determines the type of array elements.
原型对象语法 - 提供一个原型对象,该原型对象用于确定数组元素的类型和其他特征。
例如:
zarray =零(2,3,'uint8');
p = uint8([1 3 5; 2 4 6]);zarray =零(2,3,'喜欢',p);
After adding support for these functions to a class namedMyClass
, you can use similar syntax with that class:
zarray =零(2,3,'我的课');
或通过班级的对象:
p = myclass(...);zarray = zeros(size(p),,'喜欢',p);
MATLABuses these arguments to dispatch to the appropriate method in your class.
Array-Creation Functions That Support Overloading
The following functions support this kind of overloading.
要使用哪种语法
要创建一个默认对象的数组,该数组不需要构造函数的输入参数,请使用类名称语法。
要创建具有特定属性值的对象数组,或者如果构造函数需要其他输入,请使用原型对象提供此信息。
类可以支持类名和原型对象万博1manbetx语法。
You can implement a class name syntax with the真的
和false
功能s even though these functions do not support that syntax by default.
班级名称方法,如果原型方法不存在
如果您的类实现类名称语法,但没有为特定函数实现原型对象语法,则仍然可以调用两个语法。例如,如果您实现静态zeros
仅方法,您可以致电:
零(...,'喜欢',我的课(...))
在您调用原型对象语法的情况下,MATLAB首先搜索一种名为的方法零状
。如果MATLAB找不到此方法,它要求zeros
static method.
This feature is useful if you only need the class name to create the array. You do not need to implement both methods to support the complete array-creation function syntax. When you implement only the class name syntax, a call to a prototype object syntax is the same as the call to the class name syntax.
实施对阵列创建功能的万博1manbetx支持
利用two separate methods to support an array-creation function. One method implements the class name syntax and the other implements the prototype object syntax.
例如,支持万博1manbetxzeros
功能:
Implement the class name syntax:
零(...,'ClassName')
As a
Static
方法:方法(静止的)功能z =零(varargin)。。。endend
Implement the prototype object syntax:
零(...,'喜欢',obj)
As a
隐
method with thechar
vector'喜欢'
appended to the name.方法(Hidden)功能z = zeroslike(obj,varargin)...endend
HowMATLABInterprets the Function Call
The special support for array-creation functions results from the interpretation of the syntax.
打电话给
zeros
此形式的功能:零(...,'
ClassName
')使用此语法调用类静态方法:
ClassName.zeros(varargin {1:end-1})
打电话给
zeros
此形式的功能:零(...,'喜欢',obj)
Calls the class method with this syntax:
零状(obj,varargin{1:end-2})
万博1manbetx支持所有功能输入
对数组创建函数的输入参数可以包括数组的尺寸,该功能返回和其他参数。通常,您的方法必须支持三种情况:万博1manbetx
没有尺寸输入参数导致标量返回。例如:
z = zeros('我的课');
One or more dimensions equal to or less than zero, resulting in an empty array. For example:
z =零(2,0,'我的课');
任何有效数组尺寸指定数组大小。例如:
z = zeros(2,3,5,'我的课');
当数组创建函数调用您的类方法时,它传递了输入参数,不包括类名称或文字'喜欢'
和the object variable to your method. You can implement your methods with these signatures:
零(varargin)
for “class name” methods0 (obj,变长度输入宗量)
对于“喜欢原型对象”方法
样本类
TheColor
类代表特定颜色空间中的颜色,例如RGB
,HSV
, and so on. The discussions in类名称方法实现和原型对象方法实现将此类用作超载方法实现的基础。
classdefColor特性ColorValues = [0,0,0] ColorSpace ='RGB'end方法功能obj =颜色(cspace,值)如果nargin > 0 obj.ColorSpace = cSpace; obj.ColorValues = values;endendendend
类名称方法实现
Thezeros
功能strips the finalClassName
char
向量并使用它来形成对静态方法的调用Color
class. The arguments passed to the static method are the array dimension arguments.
这是一个实现zeros
method for theColor
class. This implementation:
Defines the
zeros
method asStatic
(required)Returns a scalar
Color
object if the call tozeros
没有维度参数Returns an empty array if the call to
zeros
具有等于0的任何维度参数。Returns an array of default
Color
对象。利用repmat
创建由调用对的尺寸的数组zeros
。
classdefColor。。。方法(静止的)功能z =零(varargin)如果(nargin == 0)零(“颜色”)的%z =颜色;别的如果any([varargin{:}] <= 0)任何维度<= 0的零的%z = color.empty(varargin {:});别的零(m,n,...,'颜色')的%% Use property default valuesz = repmat(颜色,varargin {:});endendendend
Thezeros
method uses default values for theColorValues
property because these values are appropriate for this application. An implementation of a那些
method can set theColorValues
property to[1,1,1]
, 例如。
Suppose that you want to overload therandi
实现以下目标的功能:
Define each
ColorValue
property as a 1-by-3 array in the range of 1 to a specified maximum value (for example, 1–255).可容纳标量,空和多维阵列大小。
Return an array of
Color
指定尺寸的对象,每个尺寸都随机ColorValues
。
classdefColor。。。方法(静止的)功能r = randi(varargin)如果(nargin == 0)randi('className')的%r =颜色('RGB',兰迪(255,[1,3]));别的如果any([varargin{2:end}] <= 0)任何维度<= 0的Randi的%r = Color.empty(varargin{2:end});别的randi(最大,m,n,...,'className')的%如果numel([varargin{:}]) < 2 error('没有足够的输入参数')enddims = [varargin {2:end}];r =零(昏暗,'颜色');fork = 1:prod(dims) r(k) = Color('RGB',randi(varargin{1},[1,3]));endendendendend
原型对象方法实现
The objective of a method that returns an array of objects that are “like a prototype object” depends on the requirements of the class. For theColor
班级,zeroLike
方法创建具有ColorSpace
property value of the prototype object, but theColorValues
全部为零。
这是一个实现零状
method for theColor
class. This implementation:
Defines the
零状
method as隐
Returns a scalar
Color
对象如果呼叫zeros
功能没有维度参数如果呼叫到
zeros
函数具有阴性或等于0的任何维度参数。Returns an array of
Color
objects of the dimensions specified by the call to thezeros
功能。
classdefColor。。。方法(Hidden)功能z = zerosLike(obj,varargin)如果nargin == 1零('like',obj)的%%cspace = obj.colorspace;z =颜色;Z.ColorSpace = CSPACE;别的如果any([varargin{:}] <= 0)任何维度<= 0的零的%z = color.empty(varargin {:});别的% For zeros(m,n,...,'like',obj)如果~isscalar(obj) error('Prototype object must be scalar')endobj = color(obj.colorspace,zeros(1,3,'喜欢',obj.ColorValues));z = repmat(obj,varargin {:});endendendend
Full Class Listing
这里是Color
具有超载方法的类定义。
不te
在实际实践中,Color
class requires error checking, color space conversions, and so on. This overly simplified version illustrates the implementation of the overloaded methods.
classdefColor特性ColorValues = [0,0,0] ColorSpace ='RGB'end方法功能obj =颜色(cspace,值)如果nargin > 0 obj.ColorSpace = cSpace; obj.ColorValues = values;endendend方法(静止的)功能z =零(varargin)如果(nargin == 0)% For zeros('ClassName')z =颜色;别的如果any([varargin{:}] <= 0)任何维度<= 0的零的%z = color.empty(varargin {:});别的零(m,n,...,'className')的%%% Use property default valuesz = repmat(颜色,varargin {:});endend功能r = randi(varargin)如果(nargin == 0)randi('className')的%r =颜色('RGB',兰迪(255,[1,3]));别的如果any([varargin{2:end}] <= 0)任何维度<= 0的Randi的%r = Color.empty(varargin{2:end});别的randi(最大,m,n,...,'className')的%如果numel([varargin{:}]) < 2 error('没有足够的输入参数')enddims = [varargin {2:end}];r =零(昏暗,'颜色');fork = 1:prod(dims) r(k) = Color('RGB',randi(varargin{1},[1,3]));endendendend方法(Hidden)功能z = zerosLike(obj,varargin)如果nargin == 1零('like',obj)的%%cspace = obj.colorspace;z =颜色;Z.ColorSpace = CSPACE;别的如果any([varargin{:}] <= 0)任何维度<= 0的零的%z = color.empty(varargin {:});别的% For zeros(m,n,...,'like',obj)如果~isscalar(obj) error('Prototype object must be scalar')endobj = color(obj.colorspace,zeros(1,3,'喜欢',obj.ColorValues));z = repmat(obj,varargin {:});endendendend