Scala code in one day (5) and scala code in one day

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.