阵列创建功能万博1manbetx的集体支持
Extend Array-Creation Functions for Your Class
There are several MATLAB®创建特定尺寸和类型的数组的功能,例如那些
andzeros
。用户定义的类可以添加对数组创建功能的支持,而无需使用超载方万博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
下面的代码可以a built-in type during initial development, and then be replaced by a user-defined class that transparently overloadszeros
:
cls = class(x);zarray =零(m,n,cls);
Array-creation functions create arrays of a specific type in two ways:
Class name syntax — Specify class name that determines the type of array elements.
原型对象语法 - 提供一个原型对象,该原型对象用于确定数组元素的类型和其他特征。
例如:
zarray =零(2,3,“你int8');
p = uint8([1 3 5; 2 4 6]); zArray = zeros(2,3,'喜欢',p);
After adding support for these functions to a class namedMyClass
, you can use similar syntax with that class:
zarray =零(2,3,'我的课');
Or pass an object of your class:
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
这些功能支持这种超载:万博1manbetx
支持超载的标量功能万博1manbetx
这些功能还支持类似的过载,但输出始终是标量万博1manbetx(或1 by-1稀疏矩阵):
对于这些功能,在创建特定类型的标量时,您无需指定大小。例如:
d = eps('单身的');
p =单个([1 3 5; 2 4 6]);d = eps('喜欢',p);
After adding support for these functions to a user-defined class, you can use similar syntax with that class as well.
要使用哪种语法
要创建一个默认对象的数组,该数组不需要构造函数的输入参数,请使用类名称语法。
要创建具有特定属性值的对象数组,或者如果构造函数需要其他输入,请使用原型对象提供此信息。
类可以支持类名和原型对象万博1manbetx语法。
You can implement a class name syntax with the真的
andfalse
功能s even though these functions do not support that syntax by default.
班级名称方法,如果原型方法不存在
如果您的类实现类名称语法,但没有为特定函数实现原型对象语法,则仍然可以调用两个语法。例如,如果您实现静态zeros
仅方法,您可以致电:
零(...,'喜欢',MyClass(...))
在您调用原型对象语法的情况下,MATLAB首先搜索一种名为的方法zerosLike
。如果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支持
Use 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:
0(…,“喜欢”,obj)
As a
隐
method with thechar
vector'喜欢'
appended to the name.方法(隐藏)功能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:
zerosLike(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,'我的课');
当数组创建函数调用您的类方法时,它传递了输入参数,不包括类名称或文字'喜欢'
and the object variable to your method. You can implement your methods with these signatures:
零(varargin)
for “class name” methods0 (obj,变长度输入宗量)
对于“喜欢原型对象”方法
样本类
TheColor
class represents a color in a specific color space, such as,RGB
,HSV
, and so on. The discussions inClass Name Method Implementationsand原型对象方法实现将此类用作超载方法实现的基础。
classdefColorpropertiesColorValues = [0,0,0] ColorSpace ='RGB'end方法功能obj =颜色(cspace,值)如果nargin > 0 obj.ColorSpace = cSpace; obj.ColorValues = values;endendendend
Class Name Method Implementations
Thezeros
功能strips the finalClassName
char
向量并使用它来形成对静态方法的调用Color
class. The arguments passed to the static method are the array dimension arguments.
Here is an implementation of azeros
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
has any dimensions arguments equal to 0.Returns an array of default
Color
objects. Userepmat
创建由调用对的尺寸的数组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]
, for example.
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).Accommodate scalar, empty, and multidimensional array sizes.
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错误('没有足够的输入参数')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
全部为零。
Here is an implementation of azerosLike
method for theColor
class. This implementation:
Defines the
zerosLike
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
class definition with the overloaded methods.
Note
在实际实践中,Color
class requires error checking, color space conversions, and so on. This overly simplified version illustrates the implementation of the overloaded methods.
classdefColorpropertiesColorValues = [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错误('没有足够的输入参数')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