I have a list of similar classes that are all children of the same abstract class. I also have an array of Booleans that should correspond to which class should be used.
For example, I have a bunch of classes, named with the following convention:
boolean[] classesOn = new boolean[4];
abstract class myClass {}
class myClass1 extends myClass { public void myClass1(float x, float y) ...}
class myClass2 extends myClass {}
class myClass3 extends myClass {}
class myClass4 extends myClass {}
...
The idea is to only use the classes that have the corresponding boolean on in classesOn
. To do this I use a for
loop that iterates through classesOn
and checks which are true
. Right now I have the following code:
for (int i = 0; i < classesOn.length; i++) {
if (classesOn[i]) {
switch (i) {
case 0: c = new myClass1(x, y); break;
case 1: c = new myClass2(x, y); break;
...
Now this is extremely inefficient and as I add new extensions of myClass
I need to add a new case. I'd like to be able to just say if (classesOn[i]) { c = new "myClass" + (i + 1) ();}
to create an instance of that particular class.
How can I do this?
(btw, these are just examples, and the actual implementation of each class varies greatly)
The use I'm currently working on is actually in Processing, where there are multiple color schemes each represented in a class. But I'm curious how to do this for all types of classes in the future as well.
The exact code that I'm working on right now looks like this - (but I'm interested in the answer in general)
abstract class Scheme {
float red,blue,green,x,y;
String description;
public void mousespot(){
this.x = mouseX;
this.y = mouseY;
return;
}
public float getRed(){
return this.red;
}
public float getBlue(){
return this.blue;
}
public float getGreen(){
return this.green;
}
public String getDescription(){
fill(255,255,255);
textSize(32);
return this.description;
}
}
class Scheme1 extends Scheme {
public Scheme1(float x, float y) {
this.description = "Green-Yellow-GW-Turqouise";
this.red = map(x, 0, width, 0, 255);
this.blue = map(y, 0, height, 0, 255);
this.green = 255 * (float) dist(width/2, height/2, x, y) / (x / y);
}
}
class Scheme2 extends Scheme {
public Scheme2(float x, float y) {
this.description = "Red-Yellow-Peach-Magenta";
this.green = map(x, 0, width, 0, 255);
this.blue = map(y, 0, height, 0, 255);
this.red = 255 * (float) dist(width/2, height/2, x, y) / (x / y);
}
}
and in the mouseDragged()
method:
for (i = 0; i < colorschemesOn.length;i++) {
if (colorschemesOn[i]) {
switch(i) {
case 0:
public Scheme selectedScheme = new Scheme1(mouseX,mouseY);
break;
case 1:
public Scheme selectedScheme = new Scheme2(mouseX,mouseY);
break;
case 2:
public Scheme selectedScheme = new Scheme3(mouseX,mouseY);
break;
case 3:
public Scheme selectedScheme = new Scheme4(mouseX,mouseY);
break;
case 4:
public Scheme selectedScheme = new Scheme5(mouseX,mouseY);
break;
case 5:
public Scheme selectedScheme = new Scheme6(mouseX,mouseY);
break;
case 6:
public Scheme selectedScheme = new Scheme7(mouseX,mouseY);
break;
case 7:
public Scheme selectedScheme = new Scheme8(mouseX,mouseY);
break;
case 8:
public Scheme selectedScheme = new Scheme9(mouseX,mouseY);
break;
case 9:
public Scheme selectedScheme = new Scheme10(mouseX,mouseY);
break;
case 10:
public Scheme selectedScheme = new Scheme11(mouseX,mouseY);
break;
case 11:
public Scheme selectedScheme = new Scheme12(mouseX,mouseY);
break;
default:
public Scheme selectedScheme = new Scheme1(mouseX,mouseY);
break;
}
}
}