Learn a little bit of Scala's apply method every day

Source: Internet
Author: User

    • Where does the Apply method usually appear?

This method is typically implemented in the associated object.

    • Objective:

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

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.