This method is typically implemented in the associated object.
changing the way the associated class instances are created, no longer through new , but directly using the class name () ,the Scala bottom implicitly calls the Apply Method
Package com.xej.learning.apply/** * Apply method: When you create an object, you no longer use the form of new, but instead use the name of the class () directly */private[apply] class Stormobject storm{//Creates an object of the associated class, implicitly calls this def Apply:storm = new Storm ()}private[apply] class Kafka (Val name:string) object kafka{ Because the main constructor of the associated class is parameterized,//So here, there must also be a parameter of Def apply (name:string): Kafka = new Kafka (name)}object applytest {def main (AR Gs:array[string]): Unit = {val Storm = storm val Kafka = Kafka ("Kafka")}}
Marathon source code, the same amount of use of this form, such as:
650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/05/A0/wKiom1morjrAOtRjAADPtPKhkqY974.png-wh_500x0-wm_ 3-wmp_4-s_3160829435.png "title=" 001.png "alt=" Wkiom1morjraotrjaadptpkhkqy974.png-wh_50 "/>
This article is from the "Xej Distributed Studio" blog, so be sure to keep this source http://xingej.blog.51cto.com/7912529/1961584
Learn a little bit of Scala's apply method every day