The understanding of object and class in Scala---The Apply method is the initialization method

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.

Too much detail, or the code in the comments.

[Java]View PlainCopy
  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. }

Constructor details for class

[Java]View PlainCopy
  1. 1. The main constructor after the class name, the parameter is declared field, if the parameter is not used Var or Val declaration, it will be declared private field
  2. 2. When instantiating a class, the statements in the class are executed: println ("person")
  3. Class person (Name:string,var Age:int) {
  4. println ("person")
  5. Def show (): Unit = {
  6. println ("show..") +name)
  7. }
  8. var gender:string=_
  9. //Secondary constructor must call the main constructor, parameter cannot use Var
  10. def this (Name:string,age:int, gender:string) {
  11. This (name,age)
  12. This.gender=gender
  13. }
  14. }

2.scala has no static modifier, but the member under object is static, and if there is a class with the same name, it acts as its companion 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
    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. }

Use of 3.Apply

[Java]View PlainCopy
  1. Class applytest{
  2. Val name="Clow";
  3. def apply () {
  4. println ("Class applytest--apply () ...");
  5. }
  6. }
  7. The members under object are static by default
  8. Object applytest{
  9. def apply () = {
  10. println ("Object applytest--apply () ...");
  11. new Applytest ()
  12. }
  13. }
  14. Object Basic4 {
  15. def main (args:array[string]) {
  16. //class name () called the Apply method under the corresponding object
  17. var a1=applytest ()
  18. println (A1.name)
  19. //Object name (), call the Apply method of the corresponding class
  20. A1 () //Output: Class applytest--apply () ...
  21. }
  22. }

Apply how to use it to implement a singleton mode

[Java]View PlainCopy
    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 understanding of object and class in Scala---The Apply method is the initialization method

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.