由于构造函数参数(属性)在基类和派生类中具有相同名称并在派生方
发布时间:2020-12-16 18:07:11 所属栏目:安全 来源:网络整理
导读:如果没有重命名B类主构造函数中的构造函数参数,我可以对以下代码进行哪些更改(不更改其函数),以便 Scala成功编译它? 例: class A(var a: Int)class B(a: Int) extends A(a) { def inc(value: Int) { this.a += value }} 错误: $scala construct.scalacons
如果没有重命名B类主构造函数中的构造函数参数,我可以对以下代码进行哪些更改(不更改其函数),以便
Scala成功编译它?
例: class A(var a: Int) class B(a: Int) extends A(a) { def inc(value: Int) { this.a += value } } 错误: $scala construct.scala construct.scala:3: error: reassignment to val def inc(value: Int) { this.a += value } ^ one error found 我在answer中将此问题提交到我之前的问题“In Scala,how do you define a local parameter in the primary constructor of a class?”. 解决方法class A(var a: Int) class B(a: Int) extends A(a) { def inc(value: Int) { (this: A).a += value } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |