我正在尝试在Matlab中创建一些对象。我习惯用Java编程,但能够在Matlab中完成这项工作对我的研究很有帮助。
所以,我有一个特定的课程:
classdef Lifter < handle
properties
weightClass = 0;
mean = 0;
var = 0;
nation = '';
end
methods
function Lifter = Lifter(weightClass,mean,var,nation) %Constructor
Lifter.weightClass = weightClass;
Lifter.mean = mean;
Lifter.var = var;
Lifter.nation = nation;
end
function set.weightClass(this,weightClass)
end
function set.mean(this,mean)
end
function set.var(this,var)
end
function set.nation(this,nation)
end
function value = get.weightClass(this)
value = this.weightClass;
end
function value = get.mean(this)
value = this.mean;
end
function var = get.var(this)
var = this.mean;
end
function nation = get.nation(this)
nation = this.nation;
end
end
端
相当标准,并没有真正做很多事情。
所以,在另一页中,我有:
function Competition()
Lifter1 = Lifter(56,1,1,'USA')
end
然而,运行它会给出:
Lifter1 =
Lifter with properties:
weightClass: 0
mean: 0
var: 0
nation: ''
有任何帮助来弄清楚为什么没有从构造函数中正确初始化这些值会非常有帮助。
此外,如何在我的Lifter1对象上实际调用setter或getter方法的示例将会很有帮助。谢谢!
答案 0 :(得分:1)
这些setter用于依赖属性(声明为create or replace trigger MAXSOME
before insert on SOMENUMBERS for each row
declare
cursor pointer is
select max(NUM) from SOMENUMBERS;
x number;
begin
x := :new.NUM;
if x > pointer.num then
dbms_output.put_line('The new number ' || x || ' is greater than the greatest number in the table.');
elsif x := pointer then
dbms_output.put_line('The new number ' || x || ' is the same as the greatest number in the table.');
else
dbms_output.put_line(pointer.num || ' is still the largest number in the table.');
end if;
end;
/
)。但是,您的安装人员没有实施(空),这意味着您的所有财产分配都不会做任何事情。这意味着属性保留在properties(Dependent)
部分中声明的默认值。
稍后编辑
如果我必须实施该课程,请按以下代码执行:
properties
其余属性的代码类似。测试:
classdef Lifter < handle
% --- PUBLIC SECTION ---
properties(Dependent)
weight_class;
end;
methods
function obj = Lifter(weight_class)
obj.weight_class = weight_class;
end;
function set.weight_class(obj, weight_class)
if ~isa(obj, 'Lifter') ...
|| ~isscalar(obj) ...
|| ~isvalid(obj)
error('Lifter:Arg', '"obj" must be a scalar valid handle object of class "Lifter".');
elseif ~isnumeric(weight_class) ...
|| ~isscalar(weight_class) ...
|| ~isreal(weight_class) ...
|| (weight_class <= 0) % + other value tests
error('Lifter:Arg', '"weight_class" must be a numeric positive scalar.');
else
obj.weight_class_ = double(weight_class);
end;
end;
function val = get.weight_class(obj)
val = obj.weight_class_;
end;
end;
% --- PRIVATE SECTION ---
properties(Access=private)
weight_class_;
end;
end
答案 1 :(得分:0)
你的主持人和吸气者不应该包含点(虽然我同意如果它有效会更优雅你的方式):
function set.weightClass(this,weightClass)
end
可能(应该):
function set_weightClass(this,weightClass)
end
如果我更换所有这些,它就能正常工作。