【发布时间】:2015-11-26 15:29:50
【问题描述】:
testcalss.m:
classdef testclass
properties(Access = public)
a;
F;
end
methods(Access = public)
function this = testclass()
if (1 == 1)
this.F = eval('@(x)a * x');
eval('this.a = 5');
end
end
function Calculate(this)
a = this.a;
this.F(1);
end
end
end
test1.m:
global solver;
solver = testclass();
solver.Calculate();
我执行测试,然后我收到这样的消息:
未定义的函数或变量“a”。 testclass/testclass/@(x)ax 中的错误 测试类/计算错误(第 18 行) 这个.F(1); test1 中的错误(第 3 行) 求解器.Calculate();*
【问题讨论】:
-
你写过构造函数吗?您确定
this语法在 MATLAB 上有效吗? -
是的,这个语法有效。有一个构造函数。你可以这样做:
global solver; a = solver.a; b = solver.b; c = solver.c。在它之前,当我创建我的类时,我写了一行global solver; solver = Parabolic();。 -
你能把它编辑成你的问题吗?而且,可以提供一些可运行的东西吗?
-
@lhcgeneva 我修复了我的帖子。请再看一遍
标签: matlab class oop global-variables anonymous-function