Scala: Use multiple constructors from Java in Scala

戏子无情 提交于 2019-12-11 02:22:23

问题


I want to use a Jar in Scala, that is written in Java.

There are classes, that have multiple constructores, for example:

public LabeledDock(Parent<? super Labeled> parent, int index, Class<?> subtype){}

and

public LabeledDock(Parent<? super Labeled> parent, Class<?> subtype)

So the first constructor has 3 inputs, the second only 2 inputs.

If i want to use these constructors in Scala in that way:

val button = new LabeledDock(scene.asParent(), classOf[Button])

Scala tells me that "ambiguous reference to overloaded definition"

If i use

val button = new LabeledDock(scene.asParent(), 0, classOf[Button])

all works fine. So i think with the first variable declaration Scala doesn't know which constructor he should use, because they are similar to each other. How can i use the constructor with only 2 inputs instead of adding the third input.

Thanks for your help!


回答1:


Now, with the help from a workmate, i have solved that problem.

Instead of

classOf[Button]

i have to use

classOf[Button].asInstanceOf[Class[_]]

With this it works fine.



来源:https://stackoverflow.com/questions/18337509/scala-use-multiple-constructors-from-java-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!