评估与类/实例字段名称对应的String

时间:2014-01-28 12:49:12

标签: java opencv

在Java OpenCV API中,使用以下工厂方法创建FeatureDetector:

public static FeatureDetector create(int detectorType)

其中detectorType对应同一个班级static final int的{​​{1}}字段,例如FeatureDetector

我想在命令行参数或配置文件中指定FeatureDetector的类型。所以基本上我需要将字符串FeatureDetector.ORB“转换”为字段FeatureDetector.ORB的值。

到目前为止,我已经能够使用反射来做到这一点:

FeatureDetector.ORB

,但我想知道是否有更快更容易的方法来实现它。

1 个答案:

答案 0 :(得分:0)

您可以使用java枚举:

enum FeatureDetectorType {
    ORB(FeatureDetector.ORB),
    FOO(FeatureDetector.FOO),
    BAR(FeatureDetector.BAR);

    private final int detectorId;

    FeatureDetectorType(detectorId) {
        this.detectorId = detectorId;
    }

    public int getDetectorId() {
        return detectorId;
    }

}

按名称查找枚举:

FeatureDetectorType.valueOf("ORB").getDetectorId()