Scala tour-class and object details

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.

Class default is the class
person{
  var age=18  //field must be initialized ()
  def age=age//This is the method, no parameters can be omitted ()
  def Incremen () {this.age+=1}
}


class student{
  var age=20     //The underlying compiler automatically adds a public method of get and set to the private age. Can be understood as pseudo public type
  Private[this] var gender= "male"//private[this] only this class can use the
  private var name= "Clow"// Declared private, the underlying compiler automatically adds a private method of get and set for the private name
  //But can define its own property method
  def getname=this.name
  def setName (value: String) {this.name=value}
}

//constructor using
class Teacher {
  var age:int = _
  var name:string = _  Constructors that can be reserved

  /overloaded and public Teacher () {} in C # like
  def this (Age:int, name:string) {This (
    )//must be called once for the main constructor
    this.age=age
    this.name=name
  }
}

Constructor details for class

1. The main constructor after the class name, the parameter is declared field, if the argument is not using Var or Val declaration, it will be declared private field
//2. When instantiating a class, the statements in the class are executed: println ("person") class person
(Name:string,var age:int) {
  println ("person")

  def Show (): Unit = {
    println ("Show ...") +name)
  }

  var gender:string=_

  //Secondary constructor must call the main constructor, parameter cannot use VAR
  def this (Name:string,age:int, gender:string {This
    (name,age)
    This.gender=gender
  }
}


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

Object dog{
  private var age=0
  def age={
    age+=1
    Age
  }
}

class dog{
  var age1= Dog.age//dog.age is a private field of object Dog. It makes me think back to C + + 's friend class
}

Use of 3.Apply

Class applytest{

  val name= "Clow";
  def apply ()  {
    println ("Class applytest--apply () ...");}

}

The member under object defaults to the static
object applytest{
  def apply () = {
    println ("Object applytest--apply () ...");
    New Applytest ()
  }
}


object Basic4 {

  def main (args:array[string]) {
    //class name () Called the Apply method under the corresponding object
    var a1=applytest ()
    println (a1.name)
    //Object name (), called the corresponding class of the Apply method
    A1 ( //Output: Class Applytest--apply () ...
  }
}

Apply how to use it to implement a singleton mode

Class Applytest private{  //Add private hidden constructor
  def SayHello () {
    println ("Hello Jop")
  }
}

Object applytest{
  var instant:applytest=null
  def apply () ={
    if (instant==null) instant=new applytest
    Instant

  }
}

Object Applydemo {
  def main (args:array[string]) {
      val t=applytest ()
      T.sayhello ()
  }
}

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.