필터 지우기
필터 지우기

Object composition and updating property

조회 수: 1 (최근 30일)
Thierry NOVO
Thierry NOVO 2013년 7월 6일
I'm writing my first program with the OOP method during several weeks and i face with an issue with composition object and update properties of object . Here is the definition of my classes: First the class Mat:
classdef Mat < handle
properties
z_left
z_right
dz
end
properties(Dependent)
z
end
methods
% Constructor
function newMat = Mat(z_left, z_right, dz)
if nargin == 3
newMat.z_left = z_left;
newMat.z_right = z_right;
newMat.dz = dz;
end
end
function z = get.z(newMat)
z = (newMat.z_left:newMat.dz:newMat.z_right)';
end
function newMat = set.z_left(newMat,value)
newMat.z_left = value;
end
end
end
I use a function sysmeca to create an object composed of two instanciation of the class Mat:
function [ obj ] = Sysmeca( varargin )
if (nargin < 1), error('Wrong number of input arguments.'); end
%
n = length(varargin);
obj = cell(1,n);
for k=1:n
obj{k}=varargin{k};
end
end
Then the class Model:
classdef Model < handle
properties
obj;
end
properties(Dependent)
z
end
methods
function newModel = Model(obj)
newModel.obj = obj;
end
function z = get.z(newModel)
L = 0;
z = 0;
for k=1:length(newModel.obj)
z = [z ; newModel.obj{k}.z + L];
newModel.obj{k}.z_left = L;
L = newModel.obj{k}.z_left;
end
end
end
end
Here is finally the main script:
clc;
close all;
clear all;
clear classes;
cm = 1e-02;
Mat1 = Mat(0,5*cm,0.1*cm);
Mat2 = Mat(0,2*cm,0.2*cm);
sys = Sysmeca(Mat1,Mat2);
Part = Model(sys);
My problem is that i wish to update property z_left in the objects Mat1 and Mat2 inside the get.z method in the class Model by the instruction newModel.obj{k}.z_left = L;, but it doesn't work. I try to implement a set method in the class Mat definition to update the property z_left, but no success.
Can somebody help me to understand how i can update property object which is part of an object composition ?? Thanks a lot in advance.
  댓글 수: 1
Matt J
Matt J 2013년 7월 6일
편집: Matt J 2013년 7월 6일
Works fine for me. I get no error messages with or without the explicitly defined set.z_left (which looks unnecessary).

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Methods에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by