Companion class companion object in Scala description __spark

Source: Internet
Author: User
Tags object object

1, the code is as follows:

[Java]  View plain  copy  print? package com.yy.oop     /**   *  associated class and associated objects    * class  Student is the companion of the associated object object student    * object student is the companion object of the associated class class student      */        //Associated class    Class student (Var name:string,var  address:string) {          private var phone= " "     //directly accesses the private members of the associated object      def infocompobj ()  = println (" Companion object in companion class: " + student.sno"   }     //Companion object    object  student {          private var sno:int = 100      def incrementsno () ={               sno += 1 //Plus 1       sno  //return sno     }     def main (args: array[string)):  Unit = {              println ("Single Case object: " + student.incrementsno ())  //single Case object                //instantiate associated class        val obj = new student ("yy", "BJ")        obj.infocompobj ();     }  }  

2. The results are as follows

[plain] view plain copy print? Single Case object: 101 Associated Class Access companion object: 101
3. Scala apply

[Java] view plain copy print?     Package Com.yy.oop/** * Companion Class and companion Object * Class student is the companion of object student associated class * Object student is associated with class student //Companion Class Student (var Name:string,var address:string) {private var phone= "110"///Direct access to associated objects Private member Def infocompobj () = println ("Access companion object in companion class: + Student.sno)}"

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.