我想从继承类调用超类构造函数。 在matlab documentation官方的语法是:Matlab调用超类抽象构造函数的语法看起来很奇怪
obj = [email protected](ArgumentList);
但是编辑似乎警告说:
the variable `obj` might be used before it is defined.
而且,如果我尝试运行代码我得到一个错误“的左操作数” @ “必须是方法名称。”
什么可能是错的?
我想从继承类调用超类构造函数。 在matlab documentation官方的语法是:Matlab调用超类抽象构造函数的语法看起来很奇怪
obj = [email protected](ArgumentList);
但是编辑似乎警告说:
the variable `obj` might be used before it is defined.
而且,如果我尝试运行代码我得到一个错误“的左操作数” @ “必须是方法名称。”
什么可能是错的?
我发现这是由于子类构造函数名称的拼写错误造成的。问题的最小重建如下所示:
classdef SuperDemo < handle
methods
function obj = SuperDemo(opt)
disp(['in super ', opt])
end
end
end
classdef SubDemo < SuperDemo
methods
function obj = SubDemoo(opt) % NOTICE THE TYPO SubDemoo
disp(['in sub ', opt])
obj = [email protected](opt);
end
end
end
如果你打电话s = SubDemo('hello')
,你将得到错误:
Error using SubDemo Error: File: SubDemo.m Line: 5 Column: 19 "@" Within a method, a superclass method of the same name is called by saying [email protected] The left operand of "@" must be the method name.
此错误是误导,因为左操作数为obj
,而不是SubDemo
。 错误消息应该指出构造函数名称SubDemoo
与类名称SubDemo
不相同。
哦,男孩,这很烦人......我的错误是,在子类的构造函数名称有一个错字。编辑和翻译都发现这是问题所在。 – 2015-02-11 20:52:06
糟糕!这将做到这一点。 – rayryeng 2015-02-11 20:52:48