Apply Companion Object Singleton object

Source: Internet
Author: User

Apply ():

The Apply method provides a good syntax mechanism when a class or object has a primary purpose

Classclass  Fooscalaobject  foomaker {     |   New Foo     | }defined module Foomakerscala> Val newfoo == [email protected]

When written in object, use the method: Class name ()

Or

class Bar {     |   0     |  class  Barscalanew= [email protected]47711479Scala >0

When writing in class, use the method: variable name ().

Associated objects, associated classes:

In Scala, you can think of statements in object as static statements in other languages such as Java, which are owned by the class, not specific objects.

When object and class names are the same, they are referred to as the associated objects and associated classes of each other, and the statements in the associated object are static parts of the class.

The associated object is usually used as a factory pattern, i.e. it can be made into a singleton object.

Single-instance objects:

class Private {}object  Bar {  new  bar  = instance}

Apply Companion Object Singleton object

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.