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 CenterFile Exchange에서 Solver-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