美文网首页
scala学习笔记

scala学习笔记

作者: daniao159 | 来源:发表于2017-05-03 11:54 被阅读9次

    在scala中,类和它的伴生对象可以相互访问对方的私有对象

    classPerson{privatevar_age =2defage_=(num:Int) =this._age = numdefage = _agedefprintObj{ println(s"I can see${Person.obj}") }}objectPerson{// access the private class field 'age'defdouble(p:Person) = p._age *2privatevalobj ="Person's object"}objectDriver extends App{//伴生对象可以引用类的私有变量valp =newPersonprintln(p.age)  p.age =10println(Person.double(p))// prints 20//类可以引用伴生对象的私有变量p.printObj//prints I can see Person's object}

    相关文章

      网友评论

          本文标题:scala学习笔记

          本文链接:https://www.haomeiwen.com/subject/npuhtxtx.html