I have a trait that is extended by two classes. Is there any way that change made to a trait variable from one class reflect in the second class?
Let's see for a simple code like this
trait parent {
var a: Int = 0;
}
class child1 extends parent {
def setValue(n: Int) = a = n
}
class child2 extends parent {
def getValue() : Int = a
}
object parentTest extends App {
val c = new child1()
c.setValue(2)
val d = new child2()
println(d.getValue)
}
I want to have d
value printed as 2
. If not with traits, is there any other way to access the same variable across multiple classes so that changes made in one also appear to other class?