首页 文章

定义类:var,val之间的差异并将其排除在外? [重复]

提问于
浏览
2

这个问题在这里已有答案:

以下三个Scala声明之间有什么区别?我理解general distinction between val and var .

class A(x: T){ ... }
class B(val x: T){ ... }
class C(var x: T){ ... }

1 回答

  • 3

    AB 之间的区别( valvar 创建存取器):

    class A(a: Int) {}
    
    // Doesn't compile (value a is not a member of)
    // (new A(1)).a
    
    class B(val b: Int) {}
    
    (new B(1)).b                                    //> res0: Int = 1
    

相关问题