【发布时间】:2015-10-28 17:28:22
【问题描述】:
我正在尝试在 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 方法的示例也会有所帮助。谢谢!
【问题讨论】:
标签: matlab