gpt4 book ai didi

java - 如何从一组类创建一个类的实例?

转载 作者:行者123 更新时间:2023-12-02 14:06:17 27 4
gpt4 key购买 nike

我有一个相似类的列表,它们都是同一个抽象类的子类。我还有一个 boolean 数组,它们应该对应于应该使用哪个类。

例如,我有一堆类,按以下约定命名:

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 {}
...

这个想法是只使用在 classesOn 中具有相应 boolean 值的类。为此,我使用 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) ();} 来创建该特定类的实例。

我该怎么做?

(顺便说一句,这些只是示例,每个类的实际实现差异很大)

我目前正在研究的用途实际上是在处理中,其中有多种颜色方案,每个颜色方案都在一个类中表示。但我很好奇如何在未来的所有类型的类(class)中做到这一点。

我现在正在处理的确切代码如下所示 - (但我对一般答案感兴趣)

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;
}
}
}

最佳答案

不要依赖命名约定,而是创建一个数组:

Class<? extends myClass>[] classes = new Class<? extends myClass>[] { 
myClass1.class, myClass2.class, myClass3.class, myClass4.class
};
boolean[] classesOn = new boolean[classes.length];

然后你可以用反射实例化它们:

if (classesOn[i])  { myClass c = classes[i].getConstructor().newInstance(); }

如果你的构造函数接受参数:

myClass c;
if (i < classesOn.lenght && classesOn[i]) {
c = classes[i]
.getConstructor(float.class, float.class)
.newInstance(mouseX, mouseY);
} else {
c = new myClass1(mouseX, mouseY);
}

关于java - 如何从一组类创建一个类的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17202707/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com