matlab的数组、cell数组等容器可以声明其内部元素的类型么?
2 ビュー (過去 30 日間)
古いコメントを表示
比如我有一个类:
myClass
我创建了一个
a=cell(1,100);
并且每个a中都初始化一个myClass的实例
但是我在编写的时候,编辑器无法将a{1,20}视作myClass类,在用.运算符时,无法代码补全类方法,只有在运行期间,编辑器才会将a{1,20}视作myClass,并提供代码补全。
那么是否可以提前提示编辑器a内部元素的类型?
0 件のコメント
回答 (1 件)
Abhinaya Kennedy
2023 年 12 月 5 日
Hi 张力翰 刘,
由于我的母语不是粤语,我将尝试用英语回答这个问题。感谢您的理解。
As I understand, you would like to hint in advance about the type of element inside a class. In MATLAB, you can provide type hints using comments to help the editor understand the type of elements inside a cell array. This can improve code completion and provide better support for class methods. Here's how you can do it:
% Assume myClass is the class you've defined
% Create a cell array
a = cell(1, 100);
% Initialize instances of myClass in each cell
for i = 1:100
a{i} = myClass(); % Initialize with an instance of myClass
end
To provide a type hint to the MATLAB editor, you can use a comment to specify the type of elements inside the cell array:
%{
a: cell array containing instances of myClass
a{1}: myClass
%}
a{1}. % After typing the dot, the editor should provide code completion for methods of myClass
By adding this type hint comment, you can help the MATLAB editor understand the type of elements inside the cell array a. This should enable better code completion and method suggestions for the class 'myClass'.
Hope this helps!
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!