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.
0 件のコメント
回答 (2 件)
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
0 件のコメント
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!