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
How to fix Overload Resolution Ambiguity in Kotlin (no lambda)?
提问by Berry
I am having Overload Resolution Ambiguity error in this line:
我在这一行遇到了重载解析歧义错误:
departureHourChoice!!.selectionModel.select(currentHourIndex)
departureHourChoice!!.selectionModel.select(currentHourIndex)
For Reference:
以供参考:
departureHourChoice
is aChoiceBox<Int>
, which is fromjava.scene.control
currentHourIndex
is anInt
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 aChoiceBox
to 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
).
departureHourChoice
是 aChoiceBox<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
ChoiceBox
to usejava.lang.Integer
instead of Kotlin'sInt
: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)