带有多个构造函数的Java到Scala

时间:2016-02-13 18:31:58

标签: java scala multiple-constructors

我有一个Java类,我试图重写为Scala。它有3个构造函数需要可用,即使我只使用1。

public class EntityNet extends EntityThrowable {

@SuppressWarnings("unused")
public EntityNet(World world) {
    super(world);
    renderDistanceWeight = 10.0D;
}

@SuppressWarnings("unused")
public EntityNet(World world, double x, double y, double z) {
    super(world, x, y, z);
    renderDistanceWeight = 10.0D;
}

public EntityNet(World world, EntityLivingBase shooter) {
    super(world, shooter);
    renderDistanceWeight = 10.0D;
}

任何建议或指示都将不胜感激。

1 个答案:

答案 0 :(得分:0)

scala有named argumentsdefault values for arguments。这是一个例子:

class HashMap[K,V](initialCapacity:Int = 16, loadFactor:Float = 0.75f) {
}
// Uses the defaults
val m1 = new HashMap[String,Int]
// initialCapacity 20, default loadFactor
val m2= new HashMap[String,Int](20)
// overriding both
val m3 = new HashMap[String,Int](20,0.8f)
// override only the loadFactory via
// named arguments
val m4 = new HashMap[String,Int](loadFactor = 0.8f)

您可以找到更多信息here