我正在尝试创建一个这样的枚举:
classdef MoleculeType < media.Molecule
enumeration
O2 (media.ElementalComposition(media.Atom(media.AtomicWeight.Oxygen), int32(2)))
end
end
继承的分子类:
classdef Molecule < handle
properties(SetAccess = immutable)
chemicalComposition
end
...
methods
function obj = Molecule(composition)
obj.chemicalComposition = composition;
end
...
end
...
end
当我尝试从MoleculeType获取实例时,我得到“无法在其枚举块之外调用'media.MoleculeType'的构造函数。”错误。 我无法弄清楚它试图调用MoleculeType构造函数的位置,所以我得到了这个错误,因为我的代码中没有对MoleculeType构造函数的引用。
请帮帮我。提前谢谢。
提示:我之前创建了没有错误的枚举类,并且它们内部具有原始值(例如O2(32)
)。当我尝试在枚举中使用对象类型时出现问题(比如在这个问题中:O2(media.ElementalComposition)
)。我在MATLAB文档中搜索过这个,没有例子。文档既没有提供示例也没有说它不受支持。
答案 0 :(得分:1)
您是如何尝试创建枚举实例的?除非我在命令行中以错误的方式构造它,否则以下对我有用:
Molecule.m(简体)
classdef Molecule
properties(SetAccess=immutable)
Composition
end
methods
function m = Molecule(composition)
m.Composition = composition;
end
end
end
<强> MoleculeType.m 强>
classdef MoleculeType < Molecule
enumeration
O2 (32)
end
end
创建/引用枚举
>> MoleculeType % wrong way
Error using MoleculeType
Cannot call the constructor of 'MoleculeType' outside of its enumeration block.
>> MoleculeType.O2 % right way
ans =
O2
>>
希望有所帮助!