maybe i’m going about this wrong, but i have a base class with a run function, and i want each child class to be able to pass a different class type into it when they use it.
the base class is called ‘spawner’, and each child class is a variant that spawns a different class of object. so i need to be able to pass that class type into the function.
what i have is a base class run function like
void run(what do i pass in here to get passedClass){
if(someObject instanceof passedClass){
someCode();
}
}
essentially i am checking the objects that have already been spawned to see if they match the type of object i want to spawn. how do i get a reference to a class type, what type of variable even is that?
okay ignoring the weird spawner design, say i want a function that uses instanceof or some similar comparison that checks the class type of an object. i want to use this function to look through an arraylist of objects and count ow many there are of a subclass. what would i pass into the function if i wanted to vary which kind of class type it counted. ie
int count(ArrayList<BaseClass> list,??? subclass /*what do i put here*/ ){
int n = 0;
for(int i = 0;i < list.size();i ++){
if(list.get(i) instanceof subclass){
n++;
}
}
return n;
}
this actually worked on the definition side but i don’t know how to reference a class to pass in apparently lol. so my definition now has Class cls in the parameter, and that works. but it wont just let me use the name of the class to pass in like count(list,Subclass) - it says The variable “Subclass” does not exist