properties and object oriented programming

조회 수: 5 (최근 30일)
juliperde
juliperde 2012년 11월 15일
hi
I need to know that if there is way to call all properties in the class without calling their name. I write many function for all properties by one by. So I need one function and that contains all properties.
for example I use this:
classdef class
properties
x
y
z
end % properties
methods
function obj = d(obj,x)
obj.x(1).value(1)=x(1).value(1);
obj.x(2).value(2)=x(2).value(2);
end%functions
function obj = e(obj,y)
obj.y(1).time(1)=y(1).time(1);
obj.y(2).time(2)=y(2).time(2);
end%functions
function obj = e(obj,z)
obj.z(1).something(1)=z(1).something(1);
obj.z(2).something(2)=z(2).something(2);
end%functions
end%methods
end %class
but I want to use something like that:
classdef class
properties
x
y
z
end % properties
methods
function obj = xxxx(obj,allproperties)
obj.prop.1(1).value(1)=x(1).value(1);
obj.prop.1(2).value(2)=x(2).value(2);
obj.prop.2(1).time(1)=y(1).time(1);
obj.prop.2(2).time(2)=y(2).time(2);
obj.prop.3(1).something(1)=z(1).something(1);
obj.prop.3(2).something(2)=z(2).something(2);
end%functions
end%methods
end %class
is there any way to provide all properties without calling them seperatly.
thank you for now.

답변 (2개)

Matt J
Matt J 2012년 11월 15일
The organization of your properties and data looks strangely complicated.
Regardless, though, you can use dynamic field names to access properties
propnames={'x','y','z'};
for i=1:length(propnames)
obj.(propnames{i}) = whatever
end

per isakson
per isakson 2012년 11월 19일
See documentation on:
  • meta.class class describes MATLAB classes and
  • meta.class.fromName. Return meta.class object associated with named class

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by