Scala Learning Notes (5)-Classes and methods

Source: Internet
Author: User

The concepts of classes and methods in Scala are similar, and here are just some of the important concepts of Scala:

1.Public is the default access level for Scala and can be omitted.

An important feature of the method parameters in 2.Scala is that they are all Val, not Var (the argument is Val is easier to articulate, you don't need to look at the code to determine if Val is being re-assigned, and Var is not.) If you want to re-assign a parameter to a method, the result is a compilation failure.

3. If the method does not find any explicit return statements, Scala returns the last computed value in the method.

4. The recommended style for methods is actually to avoid explicit, especially multiple return statements. Instead, treat each method as an expression that creates a return value. This philosophy will encourage you to create small ways to break down larger methods into smaller methods. On the other hand, design choices depend on the design content, and Scala makes it easy to write a method with multiple, explicit return, if that's what you expect.

5. With curly braces but without an equal sign, it is essentially considered to be a method that explicitly defines the result type as unit.

def g () {"This String gets lost too"}

When you remove the equals sign in front of the method body, its result type is destined to be unit. Whatever is included in the method body is no exception, because the Scala compiler can convert any type to unit. For example, if the final result of a method is a string, but the result type of the method is declared as unit, then the string is converted to a unit and loses its value.

def f (): Unit = "This String gets lost"

6.Singleton objects

One aspect of Scala that is more object-oriented than Java is that Scala has no static members. The alternative is that Scala has a singleton: Singleton object. In addition to replacing the class keyword with the object keyword, the definition of a singleton object looks like a class definition.

As in the first chapter of the small example:

Object Demo {//One Singleton object def main (args:array[string]): Unit = {println ("Hello Scala")}}

When a singleton object shares the same name as a class, he is referred to as the associated object of the class: companion object. You must define the class and its associated objects in the same source file. The class is referred to as the associated class for this Singleton object: Companion class. The class and its associated objects can access each other's private members.

One difference between a class and a singleton object is that the singleton object has no arguments, and the class can. Because you cannot instantiate a singleton object with the New keyword, you do not have the opportunity to pass it arguments. Each singleton object is implemented as a fictitious class that is pointed to by a static variable: an instance of the synthetic class (the name of the fictitious class is the object name plus a dollar sign). As the Singleton object Checksumaccumulator the fictional class is checksumaccumulator$), they have the same initialization syntax as the Java static class. Singleton objects are initialized the first time they are accessed.

Singleton objects that do not share names with the associated class are called Orphaned objects: standalone object. You'll use it for a variety of reasons, including collecting related functional methods together, or defining an entry point for a Scala app. (The small example above is an orphaned object)

7.Scala implicitly references the members of the package Java.lang and Scala, and the members of the singleton object named Predef, to each Scala source file. Predef, which is placed in the package Scala, contains a number of useful methods. For example, when you write Pringln in a Scala source file, you actually call Predef's println. (Predef.pringln runs and calls Console.println to do the actual work.) When you write assert, you are calling Predef.assert.

In 8.Scala, you can name the. scala file arbitrarily, regardless of what Scala class or code is put in it. This differs from Java in that Java requires the same class name as the file name.

Scala compiler Scalac Xxx.scala

Scala Fast compiler FSC Xxx.scala//The first time a FSC is executed, a local server daemon is created that binds to the port on your computer.

Stop FSC background process executable Fsc–shutdown

9.Application Features

Scala offers a feature, Scala. Application, you can save some of your finger input work.

The way to use this trait is to first write "extends application" behind your singleton object name. Then instead of the main method, you can put the code that you want to put in the main method directly between the braces of the singleton object. It's so simple. You can then compile and run as you would for other programs.

Import Checksumaccumulator.calculate Object Demo extends Application {//One Singleton object println ("Hello Scala")}

This works because the trait application declares the main method with the appropriate signature and inherits it from your singleton so that it can be used like a Scala program. The code between the braces is collected into the primary constructor of the singleton object: primary constructor, which is executed when the class is initialized.

Inheriting from application is shorter than writing an explicit main method, but it also has some drawbacks. First, if you want to access the command-line arguments, you cannot use it because the args array is inaccessible. Second, because of the limitations of some JVM threading models, if your program is multithreaded, you need an explicit Main method. Finally, some JVM implementations do not optimize initialization code for objects that are executed by the application trait. So you can inherit the application trait only if your program is relatively simple and single-threaded.


Scala Learning Notes (5)-Classes and methods

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.