zoukankan      html  css  js  c++  java
  • Scala初见

    简介

    Scala是一门多范式的编程语言,一种类似java的编程语言,设计初衷是实现可伸缩的语言、并集成面向对象编程和函数式编程的各种特性。

    Hello World

    object HelloWorld{
    	def main(args : Array[String]){
    		println("HelloWorld")
    	}
    }

    参数解析函数

    private def parse(args: List[String]): Unit = args match {
      case ("--date") :: value :: tail =>
        date = new SimpleDateFormat("yyyy-MM-dd").parse(value)
        parse(tail)
    
      case ("--hour") :: value :: tail =>
        if (value.toInt >=0 && value.toInt < 24) {
          hour = value.toInt
          parse(tail)
        } else {
          System.err.println(s"Invalid input hour: $value")
          throw new InvalidArgException(s"Invalid input hour: $value")
        }
      case Nil =>
    
      case x =>
        System.err.println(s"Unknown argument: $x")
        throw new InvalidArgException(s"Unknown argument: $x")
    }
    

    反射代码样例

    /**************调用 scala class ****************************/
    val clazz = Class.forName("com.testclass")                            //构造一个需要反射类的对象
    
    clazz                                                                 //使用该对象去获取私有函数 
      .getDeclaredMethod(s"$函数名", classOf[String], classOf[String])     //并得到该函数入参的数据类型,如有多个入参,要声明多个classOf
      .invoke(clazz.newInstance(), 入参1, 入参2)                           //激活该函数,传入入参
      .asInstanceOf[String] 
    
    /**************调用 scala object ****************************/
    val clazz = Class.forName("com.testobject")
    
    clazz                                                                           
      .getDeclaredMethod(s"$函数名", classOf[String], classOf[String])
      .invoke(null, 入参1, 入参2)                                           //相当于调用java的静态成员,直接调用就行不需要再new加载
      .asInstanceOf[String]
    
    /**************调用 scala class ****************************/
    val groupManager = Class.forName((groupNode  "class").text)
            .getConstructor(classOf[String])   //活动参数为String
            .newInstance(strParam)              //初始化实例,传入String类型的strParam
            .asInstanceOf[ClassManager]      //实例类型未ClassManager,即有其他地方class ClassManager
    groupManager.initialize(initparam)      //调用initialize初始化函数
    
    // 如果有一个list的class实例。 根据优先级排序
    classManagers.toList.sortBy(_.priority).map(_.run(params1, params2))

    变量

    Scala有两种变量,val和var。val就不能再赋值了。与之对应的,var可以在它生命周期中被多次赋值。

    List列表:不可变

    Array数组:可变

    列表缓冲LIstBuffer:可变

    数组缓冲ArrayBuffer:可变

    集Set,提供了可变和不可变

    Map,提供了可变和不可变

    def mkString: String
    def mkString(sep: String): String
    def mkString(start: String, sep: String, end: String): String

    函数

    Scala | map() method

    A collection in Scala is a data structure which holds a group of objects. Examples of collections include Arrays, Lists, etc. We can apply some transformations to these collections using several methods. One such widely used method offered by Scala is map().
    Important points about map() method:

      • map() is a higher order function.
      • Every collection object has the map() method.
      • map() takes some function as a parameter.
      • map() applies the function to every element of the source collection.
      • map() returns a new collection of the same type as the source collection.

    Syntax:

    collection = (e1, e2, e3, ...)
    
    //func is some function
    collection.map(func)
    
    //returns collection(func(e1), func(e2), func(e3), ...)

    参考资料

    官网:https://www.scala-lang.org/

    fold方法:https://blog.csdn.net/Next__One/article/details/77650135

  • 相关阅读:
    linux php5.6 安装Redis扩展
    linux php7.2安装扩展memcached
    极简的switch控件
    整理了最近百年的藏历数据,做了个公历藏历映射的小工具
    我是怎么让全国最大的儿童失踪预警平台流量掉底的
    jq的getScript函数不支持chaset?override掉!
    大家好像都比较少关心webcrypto,试试写个简单的sha1/sha256/sha384/sha512实现看看
    惊喜:opera换webkit内核后完美支持SDCH压缩协议
    TCPIP协议实践:wireshark抓包分析之链路层与网络层
    使用unity3d和tensorflow实现基于姿态估计的体感游戏
  • 原文地址:https://www.cnblogs.com/cfox/p/14395343.html
Copyright © 2011-2022 走看看