我有一个类似类的列表,它们都是同一个抽象类的子类。我还有一个布尔数组,应该对应于应该使用哪个类。
例如,我有一堆类,使用以下约定命名:
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 {}
...
这个想法是只使用在 in 上具有相应布尔值的类classesOn
。为此,我使用一个for
循环遍历classesOn
并检查哪些是true
. 现在我有以下代码:
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;
...
现在这是非常低效的,当我添加新的扩展时,myClass
我需要添加一个新案例。我只想说if (classesOn[i]) { c = new "myClass" + (i + 1) ();}
创建该特定类的实例。
我怎样才能做到这一点?
(顺便说一句,这些只是示例,每个类的实际实现差异很大)
我目前正在处理的用途实际上是在处理中,其中有多个配色方案,每个配色方案都在一个类中表示。但我很好奇将来如何为所有类型的课程做到这一点。
我现在正在处理的确切代码如下所示 - (但我一般对答案感兴趣)
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);
}
}
并在mouseDragged()
方法中:
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;
}
}
}