Creating a class object with the same class name as a variable

조회 수: 7 (최근 30일)
Wenjie Han
Wenjie Han 2018년 6월 20일
답변: per isakson 2018년 6월 20일
Suppose I have 10 classes defined well, with class names as a1,a2,a3...a10. A key variable K indicates which class object I should create.
For example, when K='a1',obj=a1();
when K='a2',obj=a2();
.....
I don't want to use switch or other similar control commands, since the class library is always expanding, more classes definitions will be added. I aim to find a good way to create corresponding class object with the least change, like obj=K(),where K=a1,a2,a3....but absolutely this doesn't work.
Is there any way to solve this problem? Thanks a lot!
  댓글 수: 3
Adam
Adam 2018년 6월 20일
How is 'K' created?
Ideally it should contain a function handle to the constructor of the class you want to create an object of rather than a string.
I am assuming that your classes are not really named a1, a2, a3, etc and that is just for the purposes of the question, otherwise you really need to find some meaningful names for your classes!
Guillaume
Guillaume 2018년 6월 20일
More details about the purpose of these classes would greatly help in giving you the right answer. Depending on this the answer could be: Don't do this your design is flawed, use a class factory function, use inheritance from an abstract base class, something else or a combination of these.

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

답변 (2개)

Sayyed Ahmad
Sayyed Ahmad 2018년 6월 20일
I creat some file which is called: CLASS_Template.m and used some KEY_CLASSNAME as a key in this template.
%codes of CLASS_Template:
classdef test
properties (SetAccess='public', GetAccess='public')
prop;
end
properties (SetAccess='private', GetAccess='public')
end
properties(SetAccess='private', GetAccess='private')
end
methods
function o=test(varargin)
switch nargin
case 1
o.prop = varargin{1};
otherwise
error('the number of input proprties doesn''t macht.')
end
end
function display(o)
disp('<a href="matlab: helpwin test">test properties:</a>')
disp(o.prop);
end
function disp(o)
display(o)
end
function help(o)
helpwin test
end
end
end
in the second file called CreateClass would the Key replaced with the className.
function CreateClass(varargin)
classname = varargin{1};
dirname = ['@' classname];
str_date=datestr(date,'dd.mmm.yyyy');
[status,message,messageid]=mkdir(dirname);
pause(1);
fid = fopen('/Path_to_Template/CLASS_Template.m');
F = fread(fid, '*char')';
fclose(fid);
F=strrep(F, 'KEY_CLASSNAME', classname);
F=strrep(F, 'KEY_DATE', str_date);
cd([pwd '\' dirname]);
fid = fopen([classname '.m'], 'w');
fwrite(fid, F);
fclose(fid);
open([classname '.m'])
end
Now you can run the following command
>>CreateClass test
Depending on your needs you could have more than one key.
I hope you have your answer.
cheers! Ahmad

per isakson
per isakson 2018년 6월 20일
You could create a function, factory, to communicate the intent.
K = 'a1';
obj = factory( K );
where
function obj = factory( name )
obj = feval( name );
end

카테고리

Help CenterFile Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by