"Scala" Apply and update

Source: Internet
Author: User

When we use Scala, we often use the object's apply method and the Update method.

Although we are not aware of the surface, but in fact two methods will follow the relevant conventions are called.

Apply

The Convention of the Apply method: When you pass a parenthesis to one or more arguments to a variable (object), it is converted to a call to the Apply method.

Let's look at an example first.

Class classapplytest{  //Classes The Apply method invocation example    def apply (param:string): String ={        println ("Apply method called, The paramter is "+param");        Hello,world ";//return value    }}object main{    def Main (args:array[string]): Unit ={        val myapply = new applytest;// Instantiate Applytest        println (myapply ("param1"));//Incoming parameter param1    }}
Output results

Apply Method Called,the paramter is param1

Hello,world

We see from the last example that we gave the Applytest class an object myapply, and then passed the param1 parameter to myapply.

There is no call to the object's apply method, and the Myapply object calls the Apply method itself.

Let's look at one more example.

The Apply method invocation example of the object singleobjectapplytest{//Singleton is    def apply (param1:string,param2:string): String = {        println ( "Apply method called");        param1+ "and" +PARAM2;    }    def main (args:array[string]): Unit ={        val myapply = singleobjectapplytest ("Zyh", "zqy");
Note that we are building a singleton object that is not a class and does not use the New keyword. println (myapply); }}
Output results

Apply method called

Zyh and Zqy

We define a singleton object and define the Apply method in the object.

When we set up the singleton object and pass the parameter, we automatically call the Apply method to return the param1 and param2 to the myapply.

The combination of class classandobjectapplytest{//Companion Class and Object apply method uses}class applytest{    def apply () ={        println ("Apply method in Class is called. ");    def greetingfromclass = println ("Greet method is called.");    You can omit parentheses when there are no parameters}object applytest{    def apply () = {        println ("Apply method in object is called.");        New applytest;//Returns an instance of the Applytest type    }}object classandobjectapplytest{    def main (args:array[string]): Unit ={        val myapply = Applytest ();//applytest The associated object is called, the parentheses here must not be omitted        //Note that we do not have the new keyword here, is the call to the associated object of Applytest        // The associated object automatically calls the associated object's apply method        Myapply.greetingfromclass;        Myapply ();        Applytest the associated class's invocation        //And then automatically calls the associated class's apply method    }}
Output results

Apply method in object is called.

Greet method is called.

Apply method in class is called.

Our example contains two pairs of associated classes and objects.

Because we don't have any parameters here, we use () here to pass in the parameters.

When we call the Applytest associated object and pass the argument, we automatically call the Apply method of the Applytest companion object and return an instance object of the Applytest companion class.

We can access the methods in the class by applytest the object of the associated class, although it is instantiated but does not pass arguments, so it does not call its apply method.

When we execute myapply () It is the operation of the applytest associated class, because there are no arguments, so only parentheses are added, and then the Apply method is automatically called.

Here's an example of the Apply in Scala syntax

Val Mystrarr = Array ("Bigdate", "Hadoop", "Spark");

Note that when we define an array, we don't need to create it with the New keyword, as in Java,

There is no new instance, it cannot be created with a constructor, and it is passed three parameters directly, how to create it?

In fact, Scala is automatically converted to the call of the Apply method to the associated object of the array, completing the creation and initialization of the array.

Update

Convention for the Update method: When assigning a value to an object with parentheses and including one to several parameters, the compiler invokes the object's Update method.

When called, the arguments in parentheses are used along with the object to the right of the equals sign as the input parameter of the method to execute the call.

Example

Val Mystrarr = new Array[string] (3);//declares a string array of length 3 Mystrarr (0) = "bigdate";//here exactly matches the calling rule for the Update method                         // The Update method of the array that called the associated class      //executed the Mystrarr.update (0, "bigdate")

As can be seen from the last example, the reason why the assignment of the array is not used in the same way as Java Mystrarr[0]

Instead, the assignment mechanism of the Update method is used, in order to trigger the mechanism of the Update method, the parentheses are used as Mystrarr (0) to assign the value.

"Scala" Apply and update

Related Article

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.