Class 如何在matlab中创建对象作为成员变量的类?
我在matlab中有一个项目,目录结构如下:Class 如何在matlab中创建对象作为成员变量的类?,class,matlab,member-variables,Class,Matlab,Member Variables,我在matlab中有一个项目,目录结构如下: +namespace\ @class1\ class1.m @class2\ class2.m mainfile.m 在1.m班,我有如下几点 classdef class1 %readonly variables properties(GetAccess = 'public',SetAccess = 'private') forename; last
+namespace\
@class1\
class1.m
@class2\
class2.m
mainfile.m
在1.m班,我有如下几点
classdef class1
%readonly variables
properties(GetAccess = 'public',SetAccess = 'private')
forename;
lastname;
middlename;
end
properties(Constant = true)
%in centipascals
p1 = class2(param1,param2); %this is the part I need to work
end
methods(Access = public)
function this = class1(fname,lname,mname)
this.forename = fname;
this.lastname = lname;
this.middlename = mname;
end
end
end
我似乎无法让这门课正常运转。Class1无法识别class2的构造函数(可能是因为没有正确导入某些内容)。如何导入class2,或者需要做什么才能将其他类实例作为成员变量?在Matlab中,您需要完全限定对命名空间中类的引用,甚至是来自同一命名空间中其他类的引用。像这样
classdef class1
properties (Constant = true)
%in centipascals
p1 = namespace.class2(param1,param2);
end
end
您可以从同一名称空间导入其他类,但是导入只在每个函数级别上工作,并且根本不在属性块中工作,因此它在这种特定情况下不起作用,并且可能比在其他地方更麻烦。您需要将这些目录添加到path。文件>设置路径>添加文件夹或使用addpath功能。谢谢!当我在功能块中导入它时,它工作了