【问题标题】:How to pass enum type to lambda如何将枚举类型传递给 lambda
【发布时间】:2021-03-01 16:52:08
【问题描述】:

我有一个this函数接口:

public static interface RotationDirectionCorectorGetter<MountingLocation extends Enum<MountingLocation>> {
    public Map<MountingLocation, Boolean> getModuleRotationDirectionCorrections(Map<MountingLocation, ModuleRotationVectors> rotationDirections);
}

现在我想用如下所示的函数创建它的一个实例:

public static <MountingLocation extends Enum<MountingLocation>> Map<MountingLocation, Boolean> getModuleRotaionDirectionCorrections(
        Map<MountingLocation, ModuleRotationVectors> rotationDirections, boolean isRobotRotating) {
}

我的第一次尝试是这样的,MountingLocations 是一个存在于同一类本地的枚举:

RotationDirectionCorectorGetter<MountingLocations> directionCorectorGetter = (Map<MountingLocations, ModuleRotationVectors> rotationDirections) -> getModuleRotaionDirectionCorrections<MountingLocations>(rotationDirections);

最终这不起作用,vscode sais:

getModuleRotaionDirectionCorrections 无法解决或不是 字段

执行此操作的正确语法是什么,或者这甚至是不可能的?

【问题讨论】:

    标签: java generics lambda


    【解决方案1】:

    我犯了一个愚蠢的错误,我没有记住该函数还需要一个布尔值而不仅仅是一个地图。对于这个问题的一般情况,您不必在 lambda 中调用的函数中指定泛型,因此解决方案如下所示:

    RotationDirectionCorectorGetter<MountingLocations> directionCorectorGetter = (Map<MountingLocations, ModuleRotationVectors> rotationDirections) -> getModuleRotaionDirectionCorrections(rotationDirections, true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      相关资源
      最近更新 更多