在构造函数上指定属性值

时间:2016-11-13 09:51:16

标签: scala

我一直在寻找这个,但我找不到任何东西。

如果我有这门课程

class A(a1:Int, b1:Int,c1:Int){}

我无法创建一个只指定构造函数中的一个值的实例,就像在groovy中一样吗?

new A(b1:1)

1 个答案:

答案 0 :(得分:1)

您可以为类参数提供默认值:

// add default values to your class parameters    
class A(a1: Int = 0, b1: Int = 0, c1: Int = 0)

// call the constructor by passing parameters of your choice
val a = new A(b1 = 1)