Scala:在Scala中使用Java中的多个构造函数 [英] Scala: Use multiple constructors from Java in Scala

查看:792
本文介绍了Scala:在Scala中使用Java中的多个构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在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个输入的构造函数,而不是添加第三个输入。



感谢您的帮助!



现在,在工友的帮助下,我已经解决了这个问题。


$ b

  classOf [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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆