Scala Apply Method Notes

Source: Internet
Author: User

There is such a cluster class in the Akka source code.

The way to use this is: Val cluster = cluster (Context.system);

As a novice in Scala, I did not find cluster (System..) This is a constructive method of the argument. Then he started to learn Scala.

Cluster is an object singleton.

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.

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.

Define a singleton object that is not a definition type (at the level of Scala's abstraction)

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, so they have the same initialization syntax as the Java static class. The Scala program specifically points out that a singleton object is initialized when it is first accessed.

Scala's apply has 2 forms, one is the associated object of apply, one is the associated class of apply, the following shows the use of the 2 in the apply.

Example code:

Class Applyoperation {}class applytest{    def apply () = println ("I am into the spark so much!!!")    def haveatry:unit ={        println ("has a try on apply")    }}object applytest{     def apply () = {          println ("I  A M into Scala so much ")        new applytest     }}object applyoperation{     def main (args:array[string]) {        Val Array = Array (1,2,3,4)        val a = Applytest ()//This is the use of object         a.haveatry         A ()//Here is the class in the Apply    }}

Run results

I am into the Scala so much
There is a try on apply
I am into the spark so much!!!

Object apply is a more common use. Mainly used to solve the problem of initialization of complex objects. It is also a single case.

(Just like the Singleton factory method in Java)

(go) Scala apply method notes

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.