How to override a mutable variable in Trait in scala?

后端 未结 4 1949
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 01:19

I\'d like override one mutable variable in Trait in constructor. But it will complain that \"overriding variable a in trait A of type Int; variable a cannot override a mutab

4条回答
  •  心在旅途
    2020-12-11 02:11

    try use val instead of var and use override. It work when A is abstract class, Like this:

    abstract class A{
      val a:Int = 0
    }
    
    class B extends A{
    override val a:Int=1
    }
    

提交回复
热议问题