我在 +mypackage\MyClass.m 下有以下类定义
classdef MyClass
properties
num
end
methods (Static)
function obj = with_five()
obj = MyClass(5);
end
end
methods
function obj = MyClass(num)
obj.num = num;
end
end
end
我使用 with_five() 作为静态工厂方法。
以下脚本应创建两个对象。
import mypackage.MyClass
class_test1 = MyClass(5);
class_test2 = MyClass.with_five();
class_test1 已创建。
对于 class_test2 它说:
Error using MyClass.with_five
Method 'with_five' is not defined for class 'MyClass' or is removed from MATLAB's search path.
Error in Testpackage (line 4)
class_test2 = MyClass.with_five();
当我将 MyClass.m 放在包文件夹之外并删除“import”语句时,它可以工作。
我究竟做错了什么?