Java - 通过 <class ? extends SuperClass> 使用参数获取抽象 class 的实例
Java - Get instances of abstract class by <class ? extends SuperClass> with parameters
如果标题比较含糊,请见谅。
请允许我详细说明我的问题:
假设我有一个名为 "Car" 的 class,它是一个抽象 class。
现在假设我有多个 Car 实例,例如 Audi、Volvo、Ferrari 等
我想将这些实例存储在一个枚举中 class,这样我就可以通过枚举轻松地检索它们。然而,问题是每个实例在其构造函数中都有一个参数,我不能将其作为最终属性放入枚举中。
我需要获取从 .class.
创建的 superclass (带有 1 个参数)的实例
伪代码
/* This is my super class */
public abstract class Car{
public Car(Object param){ }
}
/* This is my instance */
public class Volvo extends Car{
public Volvo(Object param){
super(param);
}
}
/* This is my other instance */
public class Ferrari extends Car{
public Ferrari(Object param){
super(param);
}
}
上面的代码是我制作的 classes 的正确显示。
好吧,现在枚举 class:
public enum CarType{
VOLVO(Volvo.class), FERRARI(Ferrari.class);
private Class<? extends Car> instance;
private CarType(Class<? extends Car> instance){
this.instance = instance;
}
/* This is what I tried, NOT working*/
public Car getCarInstance(Object param){
try{
return Car.class.getConstructor(instance).newInstance(param);
}catch(Exception e){
/* I didn't do bugmasking, but all the exceptions would
make this post look messy.*/
}
}
}
我需要的结果:
如果我打电话给 'CarType.VOLVO.getCarInstance("My parameter value"); '
它与 'new Volvo("my parameter value");'
相同
提前致谢。
尝试在 getCarInstance 中更改行:
return instance.getConstructor(Object.class).newInstance(param);
您不需要 Car.class
因为您已经在枚举构造函数中指定了类型。对了,不要叫它instance,叫它type.
开始吧:
public Car getCarInstance(Object param) {
try {
return type.getConstructor(Object.class).newInstance(param);
} catch (ReflectiveOperationException e) {
throw new IllegalStateException(e);
}
}
第二件事(如您所见)是如何检索正确的构造函数。如果您想了解更多,请阅读有关 reflection 的更多信息。
如果标题比较含糊,请见谅。
请允许我详细说明我的问题:
假设我有一个名为 "Car" 的 class,它是一个抽象 class。 现在假设我有多个 Car 实例,例如 Audi、Volvo、Ferrari 等
我想将这些实例存储在一个枚举中 class,这样我就可以通过枚举轻松地检索它们。然而,问题是每个实例在其构造函数中都有一个参数,我不能将其作为最终属性放入枚举中。 我需要获取从 .class.
创建的 superclass (带有 1 个参数)的实例伪代码
/* This is my super class */
public abstract class Car{
public Car(Object param){ }
}
/* This is my instance */
public class Volvo extends Car{
public Volvo(Object param){
super(param);
}
}
/* This is my other instance */
public class Ferrari extends Car{
public Ferrari(Object param){
super(param);
}
}
上面的代码是我制作的 classes 的正确显示。 好吧,现在枚举 class:
public enum CarType{
VOLVO(Volvo.class), FERRARI(Ferrari.class);
private Class<? extends Car> instance;
private CarType(Class<? extends Car> instance){
this.instance = instance;
}
/* This is what I tried, NOT working*/
public Car getCarInstance(Object param){
try{
return Car.class.getConstructor(instance).newInstance(param);
}catch(Exception e){
/* I didn't do bugmasking, but all the exceptions would
make this post look messy.*/
}
}
}
我需要的结果: 如果我打电话给 'CarType.VOLVO.getCarInstance("My parameter value"); ' 它与 'new Volvo("my parameter value");'
相同提前致谢。
尝试在 getCarInstance 中更改行:
return instance.getConstructor(Object.class).newInstance(param);
您不需要 Car.class
因为您已经在枚举构造函数中指定了类型。对了,不要叫它instance,叫它type.
开始吧:
public Car getCarInstance(Object param) {
try {
return type.getConstructor(Object.class).newInstance(param);
} catch (ReflectiveOperationException e) {
throw new IllegalStateException(e);
}
}
第二件事(如您所见)是如何检索正确的构造函数。如果您想了解更多,请阅读有关 reflection 的更多信息。