Scala - first acquaintance with scala

Scala:

      Official website: https://www.scala-lang.org/

      Scala is a pure object-oriented functional programming language, and the basic data types in scala are also objects (similar to the packaging classes of basic data types in java)

       Features:

         1. Seamlessly integrate with java and run in jvm

         2. Type inference, weak type reference, according to the context can infer the type of the declared variable

         3. Concurrency and distribution, reflected in the operation of the collection (Actor)

         4. Traits, characteristics, traits, combining the flexibility of the Java-style interface with the powerful functions of the class. Consider principled multiple inheritance. (Interface-oriented programming, similar to interfaces and abstract classes in java) Keyword trait

         5. Pattern matching;

         6. Higher-order functions

       Installation and use:

            1. Download the environment package directly from the official website, and configure the environment variables the same as java

                  The environment requires jdk8 or higher, jdk8 sometimes has problems. When an error is reported, either upgrade jdk or lower the scala version

                 For example, when an error was reported, jdk8 was also used at the time, but it was still not applicable. Finally, download a lower version of scala to solve it.

 

            2. Use IDE, official website to download eclipse integrated with Scala; or use idea to download plugin

       Grammar module

type of data

       1. Basic data types: Byte,Short,Int,Long, Float,Double,Char,Boolean

       2. String type: String

       3. Unit: means no value, similar to void

       4. Null 

       5. Nothing: all other types of subtypes, meaning no value

       6. Any: All types of superclasses, any instance belongs to the Any type

       7. AnyRef: Superclass of all reference types

       8. AnyVal: Superclass of all value types

Type definition

       var defines variables var v = 10; equivalent to var v:Int = 10

       val defines immutable variables, val v = 10

Code example: helloWord in scala

// 声明一个scala object
object TestScalaObject {

 // scala中的main方法, object中才能使用main方法
  def main(args: Array[String]): Unit = {

    // 声明变量  scala中语句后可以不带分号 ;
    var v1 = 10
    val v2 = 12   // 不可变的变量,重新复制报错
    print(v1 + v2)  // 输出22
  }
}

    Classes and objects in scala

         Classes and objects in scala can have the same name. Classes and objects with the same name are called companion classes and companion objects, respectively, and companion objects can access the private attributes of companion classes;

         The properties in object are immutable by default as val, and those declared as var are also static, and the default method is also static;

    Classes in Scala

// 声明一个scala类 ,属性必须有默认值,没有默认值需要将他设置为抽象类
// TestScalaClass(name: String,age: Int) 默认构造器
//  声明其他的构造器的时候必须调用默认构造器
 class TestScalaClass(name: String,age: Int) {
  var namex = name ;
  val agex = age;
  var gender = "";
 // 三个参数的构造器
  def this(name: String,age: Int,g: String) {
    this(name,age)
    this.gender = g
  }
  // 无参构造器
  def this(){
    // 必须调用 类上声明的 构造器
    this("",0)
  }

   Loops and judgment statements in scala

def main(args: Array[String]): Unit = {

    // 1. 使用 to 包头包尾
    for(i<- 1 to 10) {
      println(i)  // 1-10
    }
    // 2. 使用 until
    for(i <- 1 until 10) {
      print(i)   // 1-9
    }

    // 3. 加入判断
    for(i <- 1 to 10 ;if(i%2 ==0 && i >5 || i==2)){
      print(i)
    }
    // 4. if else,while ,do... while 和java一致
    // 5. scala中不能使用 ++ 例如 count++

 

Guess you like

Origin blog.csdn.net/xiaodujava/article/details/88945496