我正在使用八度音程中的对象,我想在子类集中调用超类集合方法。在GNU octave文档中,我还没有找到它的工作方式,所以我尝试使用matlab documentation语法但是我得到了下一个错误:''未定义在第20行第5列附近电话是。
¿我怎样才能正确访问超类方法?
这里是代码:
function s = set (o, varargin)
s = o;
if (length (varargin) < 2 || rem (length (varargin), 2) != 0)
error ([mfilename " :::: Expecting property/value pairs."]);
endif
while (length (varargin) > 1) #We get the first 2 pairs while exist.
prop = varargin{1};
val = varargin{2};
varargin(1:2) = [];
if (strcmp (prop, "color"))
if (ismember (val, ["black", "red", "green", "yellow", "blue", "violet", "cyan", "white"] )) #We check if val is a correct color.
s.color = val;
else
error ([mfilename " :::: Expecting the value for ""color"" to be a correct color."]);
endif
else
set@entity (s, prop,val);
endif
endwhile
endfunction
我将添加更多详细信息:
一个简单的例子可能是接下来的两个类:
try1,构造函数和方法(在他的文件夹@ try1中):
function t = try1(x)
t.n = x;
t = class (t, "try1")
endfunction
function o = op(t,x)
o = t.n + x;
endfunction
try2继承自try1,构造函数和方法(在他的文件夹@ try2中):
function t2 = try2(x)
t1 = @try1(x);
t.n = x;
t2 = class (t, "try2",t1);
endfunction
function o = op(t,x)
o = t.n - x;
endfunction
如何使用try2的实例访问try1的op方法?
谢谢:)
答案 0 :(得分:1)
如果要访问父类的构造函数,只需像通常在子类之外调用它一样。像这样:
$ cat @try1/try1.m
function t = try1 (x)
t.n = x;
t = class (t, "try1");
endfunction
$ cat @try1/op.m
function o = op (t)
disp ("op() from try1");
o = t.n + 5;
endfunction
$ cat @try2/try2.m
function t2 = try2 (x)
t1 = @try1 (x);
t.n = x;
t2 = class (t, "try2", t1);
endfunction
$ cat @try2/op.m
function o = op (t)
o = op (t.t1);
endfunction
$ octave
octave:1> try2 (5)
ans = <class try2>
octave:2> op (ans)
op() from try1
ans = 10
请参阅Object Oriented Programming上的手册部分,特别是Inheritance and Aggregation
部分