gpt4 book ai didi

java - 选择哪个 getConstructor 方法的条件

转载 作者:行者123 更新时间:2023-12-01 15:27:10 25 4
gpt4 key购买 nike

我有一个类,我在静态 block 中初始化了 HashMap 。通过 key ,我检索了一个类的值。为了为这个类创建对象。我使用构造函数类来获取构造函数并传递参数并创建对象。

我在 HashMap 中有两个类。要为 EchoExpression 创建对象,我需要传递两个参数,而对于 OutExpression 类,我只需要传递一个参数(字符串)。

问题:

根据键返回的类,我需要执行要获取和实现的构造函数,无论是带有一个参数还是两个参数的构造函数。

public class ExampleFactory {

private static HashMap<String,Class<?>> hmap = new HashMap<String,Class<?>>();

static
{
hmap.put("echo", EchoExpression.class);
hmap.put("Out", OutExpression.class);
}

public void getExpo(String key,String expression)
{
Class aClass =map.get(key);

//Constructor implementation for OutExpression where only one argument string is passed

Constructor constructor = aClass.getConstructor(new Class[]{String.class});

Object object= constructor.newInstance(expression);

//constructor for passing two arguments string for EchoExpression

Constructor constructor = aClass.getConstructor(new Class[]{String.class,Class.class});

Object object= constructor.newInstance(expression, Boolean.class);


return null;
}
}

如何在不使用 if else 的情况下从值(类)中选择要实现的类?

最佳答案

使用枚举并打开它。这是一个可执行 stub ,无需深入了解示例的反射或语法:

package com.trip.test;

import java.util.HashMap;
import java.util.Map;

public class ExampleFactory {

private static Map<String, Class<?>> hmap = new HashMap<String, Class<?>>();

static {
hmap.put("echo", EchoExpression.class);
hmap.put("Out", OutExpression.class);
}

public static void getExpo(String key, String expression) {

Class aClass = hmap.get(key);

ClassMappingEnum myType = ClassMappingEnum.getClassMappingEnum(aClass);

switch (myType) {
case ECHO_EXPRESSION:{
System.out.println(aClass.getName());
// do something
break;
}
case OUT_EXPRESSION:{
System.out.println(aClass.getName());
// do something
break;
}
case UNKNOWN:
default:
System.out.println("Bummer: " + aClass.getName());
}

}

public static void main(String[] args) {
getExpo("echo", "B");
getExpo("Out", "B");
}
}

enum ClassMappingEnum {
ECHO_EXPRESSION(EchoExpression.class), OUT_EXPRESSION(OutExpression.class), UNKNOWN(null);

private Class typeDes;

private ClassMappingEnum(Class typeDes) {
this.typeDes = typeDes;
}

public static ClassMappingEnum getClassMappingEnum(Class compare) {
for (ClassMappingEnum cme : ClassMappingEnum.values()) {
if (cme.typeDes.equals(compare)) {
return cme;
}
}
return UNKNOWN;
}


}

class EchoExpression<T> {
private String someString;
private Class<T> someClass;
public EchoExpression(String someString, Class<T> someClass) {
super();
this.someString = someString;
this.someClass = someClass;
}
public String getSomeString() {
return someString;
}
public void setSomeString(String someString) {
this.someString = someString;
}
public Class<T> getSomeClass() {
return someClass;
}
public void setSomeClass(Class<T> someClass) {
this.someClass = someClass;
}


}

class OutExpression {
private String someString;

public OutExpression(String someString) {
super();
this.someString = someString;
}

public String getSomeString() {
return someString;
}

public void setSomeString(String someString) {
this.someString = someString;
}

}

关于java - 选择哪个 getConstructor 方法的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10020269/

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