matlab类方法更新属性值,属性修改后其值自动改变回初始状态

classdef Record

%UNTITLED2 此处显示有关此类的摘要

%   此处显示详细说明

properties

timeStamp = date;%datetime('now');

x

z

end

properties(Constant)

myvalueisconstantInt32 = int32(5);

myvalueisconstantDouble = 5;

end

properties(Dependent) %r放在新的属性中,使用关键字Dependent

r

end

properties(Hidden)

var

end

methods

function obj = Record(x0) %构造函数

obj.x = x0;

end

function out = get.r(obj) %Depent 属性要放在get方法中

out = (obj.x).^2;

disp('get.r called');

end

function  obj = muliplyBy3(obj)

obj.z = [obj.x]*3;

disp('muliplyBy3 called');

disp(obj.z);

end

end

methods(Hidden)

function internalFnc()

disp('You cant see me');

end

end

end

当运行时,产生以下结果

>> record1 = Record(1)

record1 =

get.r called

Record with properties:

timeStamp: '08-Dec-2016'

x: 1

z: []

myvalueisconstantInt32: 5

myvalueisconstantDouble: 5

r: 1

>> record1.muliplyBy3

muliplyBy3 called

3

ans =

get.r called

Record with properties:

timeStamp: '08-Dec-2016'

x: 1

z: 3

myvalueisconstantInt32: 5

myvalueisconstantDouble: 5

r: 1

>> record1.z

ans =

[]

如果将函数改为如下形式,去掉返回值 ,结果也不对,不知为何,这是我第一次使用matlab的oop,请大家帮忙看一下,我的版本是2015a。

function   muliplyBy3(obj)

obj.z = [obj.x]*3;

disp('muliplyBy3 called');

disp(obj.z);

end

你可能感兴趣的:(matlab类方法更新属性值)