java 如何修复 Kotlin 中的重载分辨率歧义(无 lambda)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38779666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:45:46  来源:igfitidea点击:

How to fix Overload Resolution Ambiguity in Kotlin (no lambda)?

javajavafxkotlin

提问by Berry

I am having Overload Resolution Ambiguity error in this line:

我在这一行遇到了重载解析歧义错误:

departureHourChoice!!.selectionModel.select(currentHourIndex)

departureHourChoice!!.selectionModel.select(currentHourIndex)

For Reference:

以供参考:

  • departureHourChoiceis a ChoiceBox<Int>, which is from java.scene.control

  • currentHourIndexis an Int

  • The Overload Resolution Ambiguity happens in the .select()method; It is overloaded and can accept two kinds of parameters: (T obj)or (int index).

  • The .select()method allows for an item in a ChoiceBoxto be selected, and you can determine which one can be selected by referencing to that item or to it's index. In this case, I want it to be selected by Index (int).

  • Here is a photo of the errorenter image description here

  • departureHourChoice是 a ChoiceBox<Int>,它来自java.scene.control

  • currentHourIndex是一个 Int

  • 重载解析歧义发生在.select()方法中;它是重载的,可以接受两种参数:(T obj)(int index)

  • .select()方法允许ChoiceBox选择a 中的项目,您可以通过引用该项目或其索引来确定可以选择哪个项目。在这种情况下,我希望它由 Index ( int)选择。

  • 这是错误的照片在此处输入图片说明

How would one resolve the Overload Resolution Ambiguity?

如何解决重载解析歧义?

采纳答案by miensol

It seems that you are hit by this bugas a workaround you can :

似乎您被此错误击中,作为一种解决方法,您可以:

  • box the currentHourIndex:

    lateinit var departureHourChoice: ChoiceBox<Int>
    ...
    val currentHourIndex = 1
    departureHourChoice.selectionModel.select(currentHourIndex as Int?)
    
  • or change declaration of ChoiceBoxto use java.lang.Integerinstead of Kotlin's Int:

    lateinit var departureHourChoice: ChoiceBox<java.lang.Integer>
    ...
    val currentHourIndex = 1
    departureHourChoice.selectionModel.select(currentHourIndex)
    
  • currentHourIndex

    lateinit var departureHourChoice: ChoiceBox<Int>
    ...
    val currentHourIndex = 1
    departureHourChoice.selectionModel.select(currentHourIndex as Int?)
    
  • 或更改声明ChoiceBox为 usejava.lang.Integer而不是 Kotlin 的Int

    lateinit var departureHourChoice: ChoiceBox<java.lang.Integer>
    ...
    val currentHourIndex = 1
    departureHourChoice.selectionModel.select(currentHourIndex)
    

Further reading:

进一步阅读:

回答by Rowan Gontier

The solution for me in similar situation is to define in your import for example: import kotlin.math.sqrt as kotsqrt

在类似情况下,我的解决方案是在您的导入中定义,例如:import kotlin.math.sqrt as kotsqrt

then use as: val a = kotsqrt(2.3)

然后用作:val a = kotsqrt(2.3)

回答by voddan

Try casting to Int:

尝试投射到Int

departureHourChoice!!.selectionModel.select(currentHourIndex as Int)