如何修复Kotlin中的过载分辨率歧义(无lambda)? [英] How to fix Overload Resolution Ambiguity in Kotlin (no lambda)?
问题描述
我在此行中有超载分辨率歧义"错误:
I am having Overload Resolution Ambiguity error in this line:
departureHourChoice!!.selectionModel.select(currentHourIndex)
供参考:
-
departureHourChoice
是ChoiceBox<Int>
,来自java.scene.control
currentHourIndex
是Int
.select()
方法中发生了过载分辨率的歧义;它已重载,可以接受两种参数:(T obj)
或(int index)
.
The Overload Resolution Ambiguity happens in the .select()
method; It is overloaded and can accept two kinds of parameters: (T obj)
or (int index)
.
.select()
方法允许选择ChoiceBox
中的一项,并且您可以通过引用该项或其索引来确定可以选择的项.在这种情况下,我希望通过索引(int
)选择它.
The .select()
method allows for an item in a ChoiceBox
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
).
这是错误的照片
如何解决超载分辨率的歧义?
How would one resolve the Overload Resolution Ambiguity?
推荐答案
似乎您被此错误可以解决:
-
将
currentHourIndex
框:
lateinit var departureHourChoice: ChoiceBox<Int>
...
val currentHourIndex = 1
departureHourChoice.selectionModel.select(currentHourIndex as Int?)
或将ChoiceBox
的声明更改为使用java.lang.Integer
而不是Kotlin的Int
:
or change declaration of ChoiceBox
to use java.lang.Integer
instead of Kotlin's Int
:
lateinit var departureHourChoice: ChoiceBox<java.lang.Integer>
...
val currentHourIndex = 1
departureHourChoice.selectionModel.select(currentHourIndex)
进一步阅读:
- Why is Integer parameter of Java method mapped to Int and not platform type?
- Kotlin: What can I do when a Java library has an overload of both primitive and boxed type?
这篇关于如何修复Kotlin中的过载分辨率歧义(无lambda)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!