Scala code in one day (5) and scala code in one day
Scala code in one day (5)
To better control spark, I recently studied scala language features, mainly reading "quick learning scala" and writing down some codes that I think are useful.
Package examplesclass Person {val publicVal = 1 // automatically generate getter var publicVar = 2 // automatically generate getter and setter // customize getter and setter private var privateAge = 0 def age = privateAge // getter def age _ = (newValue: int) // setter {if (newValue> privateAge) privateAge = newValue}/** main constructor */class Person1 (name: String, age: Int) // The default values of name and age are val {def info = println ("name:" + name + ", age:" + age)} class Person11 (val name: String, private var age: Int) {def info = println ("name:" + name + ", age:" + age)}/** auxiliary constructor */class Person2 () {private var name = "" private var age = 0 def this (name: String) = {this () // call the default main constructor this. name = name} def this (name: String, age: Int) {this (name) // call another constructor this. age = age} object Example5 {def main (args: Array [String]) {val person = new Person person. age = 1 // call the setter method println (person. publicVal) // call the getter method val person1 = new Person1 ("linger", 24) // call the master constructor person1.info val person2 = new Person2 ("linger ") // call the auxiliary constructor new Person2 // call the default keyless constructor }}
Output
1name:linger,age:24
Link to this article: http://blog.csdn.net/lingerlanlan/article/details/43455585author: linger