Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 继承变量不';我不喜欢新的价值观_Scala_Variables_Inheritance - Fatal编程技术网

Scala 继承变量不';我不喜欢新的价值观

Scala 继承变量不';我不喜欢新的价值观,scala,variables,inheritance,Scala,Variables,Inheritance,有人能解释一下为什么: abstract class Super(var title: String) class Sub(title: String) extends Super(title) { def test = println(title) } val s = new Sub("a") s.test s.title = "b" s.test 印刷品: a a 而不是: a b 这很容易。您只是引用构造函数param,而不是继承的变量。您可以重命名构造函数param,或

有人能解释一下为什么:

abstract class Super(var title: String)

class Sub(title: String) extends Super(title) {
    def test = println(title)
}

val s = new Sub("a")
s.test
s.title = "b"
s.test
印刷品:

a
a
而不是:

a
b

这很容易。您只是引用构造函数param,而不是继承的变量。您可以重命名构造函数param,或者使用
this.
前缀引用var

class Sub(titleP: String) extends Super(titleP) {
    def test = println(title)
}

阿森尼说得对。看看你的Sub declaration没有将其构造函数参数声明为var。试着将其更改为
class Sub(var title:String).
并查看你得到的编译器错误。好的,我认为这不直观。是的。在原始代码中,一个新的构造函数参数在定义测试方法的点处隐藏原始参数。