The difference between class and object in Scala

Source: Internet
Author: User

1. Class

Scala classes are a bit different from classes in C #, such as declaring a field that is not priavate decorated var age,scala compiler field helps us to produce a private field and 2 public method get and set, which is similar to C # 's simple properties; If you use the private adornment , its methods will also be private. This is the so-called unified access principle.

[Java]View Plaincopy print?
  1. Class is the public level by default
  2. Class person{
  3. var age= //field must be initialized ()
  4. def age=age //This is the method, no parameters can be omitted ()
  5. Def incremen () {this.age+=1}
  6. }
  7. Class student{
  8. var age= ///The underlying compiler automatically adds a public method of get and set to the private age, which can be understood as pseudo type
  9. private[this] var gender="male" //private[this] only this class can use the
  10. Private var name="Clow" //declares private, the underlying compiler automatically adds the get and set proprietary methods for the private name
  11. //But you can define your own property methods
  12. def getname=THIS.name
  13. def setName (value:string) {This.name=value}
  14. }
  15. Use of constructors
  16. Class Teacher {
  17. var Age:int = _
  18. var name:string = _ //Can be reserved
  19. //Overloaded constructors are similar to the public Teacher () {} inside C #
  20. def this (Age:int, name:string) {
  21. This () //must be called once for the main constructor
  22. This.age=age
  23. This.name=name
  24. }
  25. }

2, Scala does not have static modifiers, but the members under object are static, if there is a class with the same name, this is its associated class. In object, you can generally do some initialization for the associated class, such as the Val Array=array (PS: It uses the Apply method) that we often use

[Java]View Plaincopy print?
    1. Object dog{
    2. private var age=0
    3. def age={
    4. age+=1
    5. Age
    6. }
    7. }
    8. Class dog{
    9. var age1=dog.age //dog.age is a private field of object Dog. It reminds me of the friend class of C + + .
    10. }

3, how to explain the use of apply? Let's see how we can use it to implement a singleton pattern.

[Java]View Plaincopy print?
  1. Class Applytest private{ //Add private hidden constructor
  2. Def SayHello () {
  3. println ("Hello Jop")
  4. }
  5. }
  6. Object applytest{
  7. var instant:applytest=Null
  8. def apply () ={
  9. if (instant==null) instant=new applytest
  10. Instant
  11. }
  12. }
  13. Object Applydemo {
  14. def main (args:array[string]) {
  15. Val T=applytest ()
  16. T.sayhello ()
  17. }
  18. }

The difference between 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.