Scala Class Learning

Source: Internet
Author: User

Scala source files can have many classes (class), which are public by default.

In Scala, declaring a field that is not decorated with priavate var Age,scala compiler will automatically help us to produce a private field and 2 public method get and set, which is similar to the simple properties of C #;

Stu.age = 10 (where age is def age = (x$1:int): Unit)

println (Stu.age) (where age is def Age:int)

If the private modifier is used, its methods will also be private. This is the so-called unified access principle.

There is also a common way to define this:

private [This] var gender = "Male"//means that the property belongs to the current instance only, even methods inside the class cannot access the property

Scala Object:
Scala does not have static modifiers, but the members under object are static, and if there is a class with the same name, it is the associated class. In object, you can usually do some initialization for the associated class.

Class constructor:

Each class has a default constructor, this (), and the overloaded (subordinate) constructors need to call the main constructor directly or indirectly (that is, call the other satellite constructors) this ()

If you want to disallow calling the main constructor this when you instantiate a class, use the private keyword when defining the class, for example: Class Teacher private{}

Scala Class Learning

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.