Note: This study originates from: DT Big Data DreamWorks (public number: Dt_spark)
In Scala , we usually use syntax similar to function calls. For example, if s is a string, then s (i) is the first character of the string . (And in java , you would write:s.charat (i).) Run the following code in idea:
println ("Hello (4)")//will print out ' o '
You can use this as an overloaded form of the () operator, and the implementation behind it is a method called apply . For example, in the documentation for the stringops class, you will find a way to:
def apply (n:int): Char
in other words, " Hello " (4) is a shorthand for the following statement:
"Hello". Apply (4)
If you look at the documentation for the BIGINT associated object, you'll see the Apply method that lets you convert a string or number to a BigInt object . For example, if a downward
BigInt ("1234567890")
is shorthand for the following statement:
Bigint.apply ("1234567890")
This statement produces a new BigInt object and does not need to use new. For example:
BigInt ("1234567890") * BIGINT ("1234567890")
Using the associated object's apply method like this is a Common way to build objects in Scala. For example,Array (1,4,3,4) returns an array with the apply method for the associated object of the array .
Let me do an exercise here:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/70/75/wKiom1W4YzCx7mf1AAGS6gZgl6M360.jpg "title=" 1-1. PNG "alt=" wkiom1w4yzcx7mf1aags6gzgl6m360.jpg "/>
Operation Result:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/70/75/wKiom1W4Y0KQuu8BAACG8hl0doc860.jpg "title=" 1-2. PNG "alt=" wkiom1w4y0kquu8baacg8hl0doc860.jpg "/>
Look at one more exercise:
1. Create a new class:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/72/wKioL1W4ZT2B2I5gAACVzFXKJkE110.jpg "title=" 2-1. PNG "alt=" wkiol1w4zt2b2i5gaacvzfxkjke110.jpg "/>
2. as an additional point, the methods placed in object objects are static methods, as follows:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/70/72/wKioL1W4ZUig-j66AAE4sFqevxg848.jpg "title=" 3-1. PNG "alt=" wkiol1w4zuig-j66aae4sfqevxg848.jpg "/>
Operation Result:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/70/72/wKioL1W4ZVTz9vClAABlz8iBYIk132.jpg "title=" 3-2. PNG "alt=" wkiol1w4zvtz9vclaablz8ibyik132.jpg "/>
3. next look at the use of the Apply method:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/70/75/wKiom1W4Y3HgSWyUAAFLeh06Ko4131.jpg "title=" 3-3. PNG "alt=" wkiom1w4y3hgswyuaafleh06ko4131.jpg "/>
Operation Result:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/72/wKioL1W4ZWjz0WeSAABr8-bsS_c058.jpg "title=" 3-4. PNG "alt=" wkiol1w4zwjz0wesaabr8-bss_c058.jpg "/>
In the above code, the use of "val a = ApplyTest1 ()" causes the invocation of the apply method to return the value of the method call, that is, applytest the instantiated object.
Class can also be used by the apply method, using the method as follows:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/72/wKioL1W4ZXnQfllhAAG2PSav-10769.jpg "title=" 3-5. PNG "alt=" wkiol1w4zxnqfllhaag2psav-10769.jpg "/>
Operation Result:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/75/wKiom1W4Y5XgiAORAABs8ItEdHA864.jpg "title=" 3-6. PNG "alt=" wkiom1w4y5xgiaoraabs8itedha864.jpg "/>
Because the methods and properties in object are static, this is the ideal vector for singleton objects, as shown in the example code:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/70/72/wKioL1W4ZYzjIBqKAAIO0Ll5BbQ797.jpg "title=" 3-7. PNG "alt=" wkiol1w4zyzjibqkaaio0ll5bbq797.jpg "/>
In other words, Object itself is a singleton object.
Resources:
DT Big Data DreamWorks public number :D t_spark
This article is from "My Heart Flying" blog, please make sure to keep this source http://10562836.blog.51cto.com/10552836/1679568
Scala Learning Review (v)----apply in Scala