Scala中的getter和setter

Created by Wang, Jerry, last modified on Sep 25, 2015

test source code:

/* 2015-09-14 15:36PM */
package test {
  class Counter {
    private var value = 0 // or else value will be treated as NaN
     def add() { value += 1 } // default: public
     def current = value // here I define a method without (),
                         // this forces the method caller to also call it without ()
     
     def printArgs(args: Array[String]): Integer = {
      args.foreach(println)
      return args.length
     }
     
     var public_value = 1
  }
 
  object jerry extends App {
    val counter = new Counter
    println("current: " + counter.current);
   
    val array = Array("1","2","3")
    println(counter.printArgs(array))
   
    counter.public_value = 41
    println(counter.public_value)
  }
}




由此可见,getter和setter并非被命名为getXXX和setXXX


要获取更多Jerry的原创文章,请关注公众号"汪子熙":

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐