properties and object oriented programming

2 次查看(过去 30 天)
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