Pass class selection to function

4 ビュー (過去 30 日間)
Florian Rössing
Florian Rössing 2021 年 8 月 16 日
コメント済み: Matt J 2021 年 8 月 19 日
Hi everyone.
I have a question on classes. Can I hand over a Classname to a function, so that the function uses that specific Class?
I could use strings and a switch statement, but is there a simpler way?
See the example below for details on how i want it to work.
Imagine i have a superclass Employes. Employes has an abstract function Setup.
classdef Employe <handle
properties (Abstract)
name;
end
methods (Abstract)
Setup(Obj,Name,arg)
end
end
I have two subclasses to Employe, one being Salesman, the other Developer. Both have a custom Setup function.
classdef Salesman < Employe
properties
sold=0;
end
methods
function Setup(Obj,Name,arg)
arguments
Obj Salesman;
Name string;
arg.sold int =0;
end
Obj.name=Name
Obj.sold=arg.sold;
end
end
end
classdef Developer < Employe
properties
language="";
end
methods
function Setup(Obj,Name,arg)
arguments
Obj Salesman;
Name string;
arg.language string ="";
end
Obj.name=Name
Obj.language=arg.language;
end
end
end
Now i want to setup my staff in a serperate function that creates an array of one kind of employes.
function Staff=CreateStaffArray(EmployeType,length)
Staff=EmployeType.empty(length,1)
%followed by some filling of Staff
.
.
.
end
Where EmployeType would either be Developer or Salesman.
Is there a way to do this?

採用された回答

Matt J
Matt J 2021 年 8 月 16 日
編集済み: Matt J 2021 年 8 月 16 日
It seems to me your setup functions should be converted to class constructors that accept zero input arguments. There is no point having argument validation if you are just going to be assigning the values to properties. You can do the validation in the property defintion blocks instead.
classdef Employe <handle
properties (Abstract)
name String;
end
end
classdef Salesman < Employee
properties
sold=0;
end
methods
function Obj=Salesman(Name,sold)
if ~nargin, return; end
Obj.name=Name;
Obj.sold=sold;
end
end
end
classdef Developer < Employee
properties
language String ="";
end
methods
function Obj=Developer(Name,language)
if ~nargin, return; end
Obj.name=Name;
Obj.language=language;
end
end
end
Once you've done this, you could simply use feval() to invoke the constructor that you want:
function Staff=CreateStaffArray(EmployeeType,N)
Staff(N)=feval(EmployeeType);
%followed by some filling of Staff
.
.
.
end
  2 件のコメント
Florian Rössing
Florian Rössing 2021 年 8 月 19 日
編集済み: Florian Rössing 2021 年 8 月 19 日
Thank you very much for helping out.
Works fine in principle. I can create the needed array like this. However all Staff(i) entries are now the same. Whenever I change one of them, they all change. Like Matlab tries to save storage and just dublicates one Staff member N times instead of making N staff members
Matt J
Matt J 2021 年 8 月 19 日
The simplest way to fix that is to make Employee a value class (instead of handle). Otherwise, you must use a loop and construct each Staff(i) individually.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSolver-Based Optimization Problem Setup についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by