Scala briefly -- class and object, scala

Source: Internet
Author: User

Scala briefly -- class and object, scala
Class

In scala, classes are not declared as public. The source file can contain multiple classes, all of which have common visibility. The getter and setter methods are provided for each field, which are called Field Names and field name_, respectively, and can be customized. Note: 1) if the field is private, getter and setter are also private. 2) if the field is val, only the getter method is used. 3) If no getter or setter is required, the field can be declared as private [this]

When the scala field is marked as @ BeanProperty, the Java attribute definition methods getxxx and setxxx are generated.

The class in scala has a primary constructor, which can have any number of auxiliary constructor. The difference between the constructor and other languages is: 1) the name is this 2) must start with a call to a previously defined third-party constructor or master constructor.

The parameters of the main constructor are placed directly after the class name, and all statements in the class definition will be executed. The non-parameter main constructor only executes all statements in the class body. If you define the master constructor as private, you must use the auxiliary constructor to construct the object. In the embedded class, you can useExternal class. thisTo access this reference of the external class.

Object

An object has all the characteristics of fatigue, and only cannot provide the constructor parameters. All the places where the singleton object is used can be implemented using objects in scala. 1) as a place to store tool functions or constants 2) efficiently share a single immutable instance 3) when a single instance is needed to coordinate a service

For Classes in Java that have both instance methods and static methods, scala is implemented through classes and companion objects with the same name as classes. Class and its associated objects can access the private properties of each other and must exist in the same source file.

Each scala program must start with the main method of an object, and can also expand the App features. Scala does not have Enumeration types, but the standard library provides an Enumeration helper class for generating Enumeration.

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.