八度:创建一个类变量,然后在类中访问(在构造函数中)
也许这是一个微不足道的问题,但我想知道如何在八度范围内访问类别的恒定属性或类功能。让我们以一个例子:
classdef Example % < FatherClass
% read-only protected properties
properties (Constant=true, Access=protected)
all_levels = {"A","B","C","D"};
endproperties
% protected properties
properties(Access=protected)
level = 'D';
output = '.';
endproperties
methods(Access=public)
function obj = Example (level,outputfilepath)
if any(strcmp(all_levels,level))
obj.level = level;
else
error ("possible levels are: A B C D");
endif
obj.output = outputfilepath
endfunction
endmethods
end
运行此类示例我收到错误:
error: 'all_levels' undefined near line 12, column 12
error: called from
Example at line 12 column 13
所以,我尝试了类似
if any(strcmp(obj.all_levels,level))
obj.level = level;
结果的尝试,也定义了一个getter:
methods (Static = true)
function lvs = gel_levels()
lvs = all_levels
endfunction
endmethods
...
methods(Access=public)
function obj = Example (obj,level,outputfilepath)
all_levels = get_levels()
% disp(all_levels)
if any(strcmp(all_levels,level))
obj.level = level;
else
error ("possible levels are: A B C D");
endif
obj.output = outputfilepath
endfunction
endmethods
对不起,但我对Octave是新手,我还没有找到任何有关的例子这。我要完成的是一个简单的类变量
Maybe it's a trivial question but I would like to know how to access a constant property within the class contructor or a class function in octave. Let's make an example:
classdef Example % < FatherClass
% read-only protected properties
properties (Constant=true, Access=protected)
all_levels = {"A","B","C","D"};
endproperties
% protected properties
properties(Access=protected)
level = 'D';
output = '.';
endproperties
methods(Access=public)
function obj = Example (level,outputfilepath)
if any(strcmp(all_levels,level))
obj.level = level;
else
error ("possible levels are: A B C D");
endif
obj.output = outputfilepath
endfunction
endmethods
end
running this class example I receive the error:
error: 'all_levels' undefined near line 12, column 12
error: called from
Example at line 12 column 13
So, I've tried something like
if any(strcmp(obj.all_levels,level))
obj.level = level;
With the same result, also defining a getter:
methods (Static = true)
function lvs = gel_levels()
lvs = all_levels
endfunction
endmethods
...
methods(Access=public)
function obj = Example (obj,level,outputfilepath)
all_levels = get_levels()
% disp(all_levels)
if any(strcmp(all_levels,level))
obj.level = level;
else
error ("possible levels are: A B C D");
endif
obj.output = outputfilepath
endfunction
endmethods
Sorry but I'm quite new to octave and I haven't found any example about this. What I'm trying to accomplish is a simple class variable
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题有点令人困惑,因为不同的尝试似乎正在使用不同的部分,但是总的来说,您认为您的问题是您不是将对象作为方法中的正式参数传递。
还不清楚您是否试图修改对象“原位”,还是试图生成新的对象...但是无论如何都不可能会记住,不可能修改对象(除非从'hander''继承来目的)。因此,您应该做的典型的事情是:将对象作为第一个输入传递给您应该使用类方法定义,对其进行修改,返回它,然后在调用中使用此方法时工作区,通过分配捕获此对象(通常在变量中以与调用工作区中的所谓对象相同的名称)。
这对我有用:
更新
更新的代码给定修订的问题/评论。
这在八度的7.1.0中对我有用
The question is a bit confusing, as different attempts seem to be using different parts, but in general I think your problem is that you are not passing the object as a formal parameter in the method.
It is also not clear if you are trying to modify the object "in-place", or trying to generate a new one ... but in any case remember that modifying objects in place is not possible (unless inheriting from the 'handle' object). Therefore the typical thing you're supposed to do is: pass the object in as the first input as you're supposed to do with class method definitions, modify it, return it, and then when you're using this method in your calling workspace, capture this object (typically in a variable by the same name as the called object in the calling workspace) via assignment.
This works for me:
UPDATE
Updated code given the revised question / comments.
This works for me in octave 7.1.0