Syntax sugar for class and object in Scala

Source: Internet
Author: User

First of all we know that in Scala class is classes, object is objects, and if class and object have the same name, they are referred to as associated objects or associated classes, such as we write a people class

Class people (Id:int, name:string) {def say = "Hello:" + name + ", my ID is:" +id}object people{def apply (ID : Int, name:string) = {println ("is apply") New people (ID, name)}}object test{def main (Args:arra Y[string]) {val p = people (2, "Tom") P say}}

In this code, I do not have new people, which is why? Because this is a benefit of the associated object, because creating the associated object does not require new keywords, and our people (2, "Tom") is actually calling the Apply method, which is a syntactic sugar in Scala, which makes the code more streamlined.


Syntax sugar for class and object in Scala

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.