Hi, I'm a newbie to matlab. The class individual as follows has a property named Genotype. I need this property to be another class which would have some other properties a constructor and functions. The property Genotype should correspond to the class genotype.
This can be easily achieved in other OO languages as java or c++ since they are type constrained. I don't understand how to do this with matlab. Any help would be appreciated
classdef Individual
properties
Genotype
end % properties
methods
function
end % function
end % methods
end % classdef
classdef Genotype
properties
X
Y
Z
end % properties
methods
function
end % function
end % methods
end % classdef

 採用された回答

Eric
Eric 2012 年 4 月 4 日

0 投票

I think the following should work for setting this property from the Constructor function. The following assumes that you have created at Genotype object called Genotype_obj.
I might recommend renaming the Genotype parameter of Individual to something else to distinguish it from the class name, but I think Matlab is smart enough to know the difference.
function obj = Individual(Genotype_obj)
assert(isa(Genotype_obj,'Genotype'),'Individual Constructor Error: Genotype_obj is of class %s, not a Genotype object.', class(Genotype_obj));
obj.Genotype = Genotype_obj;
return
The idea is to pass the object to the constructor and then check that it is the right type of object. This could also be handled in a Set() function. You might want to perform this error checking in a Set() function in addition to this code if you will ever be setting the property that way.
Good luck,
Eric

1 件のコメント

Amidnri Udugala
Amidnri Udugala 2012 年 4 月 4 日
thanx eric :) i think this is what i wanted

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Software Development についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by