Scala:在Scala中使用Java中的多个构造函数 [英] Scala: Use multiple constructors from Java in Scala
问题描述
我想在Scala中使用以Java编写的Jar。
有些类有多个构造函数,例如:
public LabeledDock(Parent< ;? super Labeled> parent,int index,Class<?> subtype){}
和
public LabeledDock ;?super Labeled> parent,Class<?>子类型)
所以第一个构造函数有3个输入,第二个只有2个输入。
如果我想在Scala中使用这些构造函数:
val button = new LabeledDock(scene.asParent(),classOf [Button])
Scala告诉我
如果我使用
val button = new LabeledDock(scene.asParent(),0,classOf [Button])
都可以正常工作。
所以我认为第一个变量声明Scala不知道他应该使用哪个构造函数,因为他们是彼此相似。如何使用只有2个输入的构造函数,而不是添加第三个输入。
感谢您的帮助!
现在,在工友的帮助下,我已经解决了这个问题。
$ bclassOf [Button]
我必须使用
classOf [Button] .asInstanceOf [Class [_]]
使用它可以正常工作。
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!
解决方案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.
这篇关于Scala:在Scala中使用Java中的多个构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文