Pass class selection to function
Ältere Kommentare anzeigen
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?
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Nearest Neighbors finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!