自定义对象索引
为您的类自定义索引引用和赋值
自定义对象的索引引用和赋值行为。用户定义的类拥有与 MATLAB® 内置类相同的索引行为,但您可以通过从一个或多个超类继承来修改此行为。
您可以通过模块化索引 mixin 类(自 R2021b 开始提供)来单独自定义各索引操作。例如,您可以通过从 matlab.mixin.indexing.RedefinesParen
继承来自定义圆括号索引,同时对圆点和花括号索引使用默认 MATLAB 行为,而不编写额外的代码。
您也可以在类中重载 subsref
和 subsasgn
函数,但这种方法要求您重载圆括号、圆点和花括号索引,即使您只需自定义一种行为也是如此。请尽可能使用模块化索引类。
类
matlab.mixin.indexing.RedefinesParen | Customize class indexing operations that use parentheses (自 R2021b 起) |
matlab.mixin.indexing.RedefinesDot | Customize class indexing operations that use dots (自 R2021b 起) |
matlab.mixin.indexing.RedefinesBrace | Customize class indexing operations that use braces (自 R2021b 起) |
matlab.indexing.IndexingOperation | Type of customized indexing operation and referenced indices (自 R2021b 起) |
matlab.mixin.Scalar | Enforce scalar behavior for class instances (自 R2021b 起) |
matlab.mixin.indexing.ForbidsPublicDotMethodCall | Disallow calling public methods using dot notation (自 R2021b 起) |
matlab.mixin.indexing.OverridesPublicDotMethodCall | 用圆点表示法调用公共方法会调用重载的点索引 (自 R2021b 起) |
函数
主题
- 自定义对象索引
默认情况下,MATLAB 类支持对象数组索引。许多类设计不要求对此行为进行修改。
- Customize Parentheses Indexing for Mapping Class
This example shows how to customize parentheses indexing for a mapping class.
- Forward Indexing Operations
Forward additional levels of indexing after customized indexing operations.
- Overload end for Classes
Overload
end
for customized indexing applications. - Objects in Index Expressions
You can design objects that can be used as indices in indexing expressions.
- Code Patterns for subsref and subsasgn Methods
There are code patterns useful for modifying object indexing.