Programming in Scala Reading Note 3

Source: Internet
Author: User

Class and Object
 
ClassTestclass {Val some= 1}

The generated class file contains a some () method, and this method is public. That is to say, you can obtain this some value through the Class Object outside the class.

Modify:

 
ClassTestclass {PrivateVal some = 1}

A private some () method is generated, and the return value is also some. However, it cannot be accessed outside the class.

 

By default, the parameter of the class method is of the Val type.

 

Most of the side effects are used to modify the value of an attribute or switch an I/O. If only the side effects exist, the returned value is unit. In this case, you should save: unit =, use {} to include the method body.

 

If a method returns a value, do not discard =. Otherwise, the return value of this method is unit, because any type can be converted to unit.

 

Semicolon Syntax:

If the arithmetic operation is not on a row, we should add the best operator at the end of each row instead of the beginning of each row, because + A returns, instead of adding a to a value.

 

Companion objects must be placed in the same file. The companion class and the companion object can access each other's private variables.

 

The difference between the companion object and the companion class is that the companion object has no construction parameters becauseAn associated object cannot be instantiated..

 

Scala Program

Scala is an object, either a companion object or a standalone object ).

This object must have a method named main that returns the unit value and has a suitable signature (the parameter is array [String.

If a Scala file ends with a definition, rather than a result expression, it is an object, otherwise it is a script ).

In any case, consider the following:

Scalac is used to compile (interpret). Scala files into. class files

Scala is used to run programs

  1. The script file must contain. Scala.
  2. If the object file is already compiled, you do not need to include. Scala.

FSC (fast Scala compiler) can enable a daemon thread and allocate a port number to compile Scala files. This is slow for the first time and will be much faster in the future.

FSC-shutdown is used to disable this daemon thread.

The problem with scalac is that the compiler is started every time it is executed, and it takes time to scan all the jar files and some other preparation work before the compilation is executed.

 

 

 

 

 

 

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.