Java 动态初始化对象

Java 动态初始化对象,java,object,Java,Object,下面的代码是一个简化版本。Write和Read是实现IAction接口的类 IAction newAction; if (userInput.equalsIgnoreCase("WRITE")){ newAction = new Write(); } else if (userInput.equalsIgnoreCase("READ")){ newAction = new Read(); } ... 如果我有许多操作要实现,那么我将不得不执行太多的If语句。所以问题是,是否有一

下面的代码是一个简化版本。
Write
Read
是实现
IAction
接口的类

IAction newAction;
if (userInput.equalsIgnoreCase("WRITE")){
    newAction = new Write();
}
else if (userInput.equalsIgnoreCase("READ")){
    newAction = new Read();
}
...

如果我有许多操作要实现,那么我将不得不执行太多的If语句。所以问题是,是否有一种方法可以自动创建每个类而不必通过所有这些if语句?

是的,这是可能的。首先创建一个对象。然后检查类名是否存在以确保userinput是有效的类,然后创建一个动态类。然后将其指定给对象

Object newAction = null;

 try {
  Class<?> clazz =  Class.forName( "your.fqdn.class."+userInput );
  Constructor<?> ctor = clazz.getConstructor(String.class);
  newAction = ctor.newInstance(new Object[] { ctorArgument });
  newAction = new your.fqdn.class."+userInput;
 } catch( ClassNotFoundException e ) {
   // catch an error if the class for the object does not exists.
}

但是要小心。出于安全原因,不建议这样做。您应该在执行此操作之前验证输入

是的,这是可能的。首先创建一个对象。然后检查类名是否存在以确保userinput是有效的类,然后创建一个动态类。然后将其指定给对象

Object newAction = null;

 try {
  Class<?> clazz =  Class.forName( "your.fqdn.class."+userInput );
  Constructor<?> ctor = clazz.getConstructor(String.class);
  newAction = ctor.newInstance(new Object[] { ctorArgument });
  newAction = new your.fqdn.class."+userInput;
 } catch( ClassNotFoundException e ) {
   // catch an error if the class for the object does not exists.
}

但是要小心。出于安全原因,不建议这样做。您应该在执行此操作之前验证输入

我取决于你所说的“自动”是什么意思。计算机自动地做事情,但不是在有人编程自动地做某事之前。你可能是说“不那么麻烦”。下面是一种使用Java8特性的方法

// Make a Map that tells what word should be coupled to what action
Map<String, Supplier<IAction>> actionMapping = new HashMap<>();
actionMapping.put("WRITE", Write::new);
actionMapping.put("READ", Read::new);

// When you get user input, simply lookup the supplier
// in the map and call get() on it
IAction action = actionMapping.get(userInput.toUpperCase()).get();
//制作一个地图,告诉什么单词应该与什么动作相结合
Map actionMapping=新建HashMap();
actionMapping.put(“WRITE”,WRITE::new);
actionMapping.put(“读取”,读取::新建);
//当您获得用户输入时,只需查找供应商即可
//在映射中,并对其调用get()
IAction action=actionMapping.get(userInput.toUpperCase()).get();
如果您不使用Java 8,可以使用稍微不同(但类似)的方法:

// Map that tells what word should be coupled to what action
Map<String, Class<? extends IAction>> actionMapping = new HashMap<>();
actionMapping.put("WRITE", Write.class);
actionMapping.put("READ", Read.class);

// Lookup the action class for the input and instantiate it
IAction action = actionMapping.get(userInput.toUpperCase()).newInstance();
//告诉什么单词应该与什么动作耦合的映射

MapI取决于你所说的“自动”是什么意思。计算机自动地做事情,但不是在有人编程自动地做某事之前。你可能是说“不那么麻烦”。下面是一种使用Java8特性的方法

// Make a Map that tells what word should be coupled to what action
Map<String, Supplier<IAction>> actionMapping = new HashMap<>();
actionMapping.put("WRITE", Write::new);
actionMapping.put("READ", Read::new);

// When you get user input, simply lookup the supplier
// in the map and call get() on it
IAction action = actionMapping.get(userInput.toUpperCase()).get();
//制作一个地图,告诉什么单词应该与什么动作相结合
Map actionMapping=新建HashMap();
actionMapping.put(“WRITE”,WRITE::new);
actionMapping.put(“读取”,读取::新建);
//当您获得用户输入时,只需查找供应商即可
//在映射中,并对其调用get()
IAction action=actionMapping.get(userInput.toUpperCase()).get();
如果您不使用Java 8,可以使用稍微不同(但类似)的方法:

// Map that tells what word should be coupled to what action
Map<String, Class<? extends IAction>> actionMapping = new HashMap<>();
actionMapping.put("WRITE", Write.class);
actionMapping.put("READ", Read.class);

// Lookup the action class for the input and instantiate it
IAction action = actionMapping.get(userInput.toUpperCase()).newInstance();
//告诉什么单词应该与什么动作耦合的映射

映射您可以创建枚举

public enum Action implements IAction{
    READ,WRITE;
}
然后像这样用一行

IAction action = Action.valueOf(userInput.toUpperCase());

您可以创建枚举

public enum Action implements IAction{
    READ,WRITE;
}
然后像这样用一行

IAction action = Action.valueOf(userInput.toUpperCase());

可以使用枚举并为每个枚举常量实现接口。下面是一个实施的示例:


无论大小写如何,您都可以使用“获取正确的常量”。

您可以使用枚举并为每个枚举常量实现接口。下面是一个实施的示例:


无论大小写如何,都可以使用get获得正确的常数。

当我考虑使用反射时,我在这里发现了许多好的解决方案和一些新想法。当我考虑使用反射时,我在这里发现了许多好的解决方案和一些新想法。