从零开始学Scala系列(二)之初识Scala
2017-09-07 11:20
309 查看
1. 学习使用scala解释器
C:\Users\rodbate>scala Welcome to Scala 2.12.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_101). Type in expressions for evaluation. Or try :help. scala> :help1
2
3
4
5
:help命令可查看命令帮助:
scala> :help All commands can be abbreviated, e.g., :he instead of :help. :edit <id>|<line> edit history :help [command] print this summary or command-specific help :history [num] show the history (optional num is commands to show) :h? <string> search the history :imports [name name ...] show import history, identifying sources of names :implicits [-v] show the implicits in scope :javap <path|class> disassemble a file or class name :line <id>|<line> place line(s) at the end of history :load <path> interpret lines in a file :paste [-raw] [path] enter paste mode or paste a file :power enable power user mode :quit exit the interpreter :replay [options] reset the repl and replay all previous commands :require <path> add a jar to the classpath :reset [options] reset the repl to its initial state, forgetting all session entries :save <path> save replayable session to a file :sh <command line> run a shell command (result is implicitly => List[String]) :settings <options> update compiler options, if possible; see reset :silent disable/enable automatic printing of results :type [-v] <expr> display the type of an expression without evaluating it :kind [-v] <expr> display the kind of expression's type :warnings show the suppressed warnings from the most recent line which had any1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
首先写个hello world 先啊!
scala> println("Hello World") Hello World1
2
命令行表达式运算 比如 1 + 2
scala> 1 + 2 res1: Int = 31
2
res1 用于保存表达式1+2的结果的变量 究竟是可变(var)类型 还是不可变(val)类型,做个试验试试:
scala> 1 + 2 res1: Int = 31
scala> println(res1)
3
scala> res1 = 2
<console>:12: error: reassignment to val
res1 = 2
scala> println(res1)
3
2
3
4
5
6
7
8
9
10
11
12
由此可知res1是val不可变的类型(不可重新赋值变量,相当于java中的final,C语言中的const修饰的变量)
res1: Int = 3冒号是变量类型和变量之间的分隔符,再由于res1是不可变的, 可相当于java中的
final int res1 = 3
scala> res1 * 9 res5: Int = 271
2
其实在scala中 + - * / % 这些运算符在scala也可以理解为函数(function) 后面细说。
2. scala中的变量种类
scala中的变量种类有两种var和valval 不可变量,常量,不可重新赋值(相当于final)
var 可变量, 可重新赋值
看两个例子
scala> val str = "hello scala" str: String = hello scala scala> print(str) hello scala scala> str = "hello" <console>:12: error: reassignment to val str = "hello" ^1
2
3
4
5
6
7
8
9
10
scala> var string = "hello scala" string: String = hello scala scala> println(string) hello scala scala> string = "changed" string: String = changed scala> println(string) changed1
2
3
4
5
6
7
8
9
10
11
这两个例子充分说明了 var 和 val两种变量的区别。
当然我们在scala中声明变量时,也可显示指定变量类型 如:
scala> val s: String = "scala" s: String = scala scala> val s1: java.lang.String = "java" s1: String = java1
2
3
4
5
和java中一样在java.lang包下的类可以直接使用简单类名引用,不需要全限定名,而且在scala中也多了一个这样包 就是scala包下的类和object的引用(后面用到细说)。
scala> val multiline = | "multiline" multiline: String = multiline scala> val ss = | | You typed two blank lines. Starting a new command.1
2
3
4
5
6
7
8
scala命令行中 多行连续可按一次enter,如果要结束多行状态,就需要连续按两次enter。
3 初始scala函数(Function)
说到scala,就会想起它的函数编程风格,那接下来就简单地来认识下scala函数吧。定义一个简单的max函数 输入两个值 求最大值
scala> def max(x: Int, y:Int): Int = { | if(x > y) x else y | } max: (x: Int, y: Int)Int scala> println(max(1,2)) 2 scala> println(max(10,2)) 101
2
3
4
5
6
7
8
9
10
11
当然上述函数还不是最简形式
scala> def max1(x: Int, y: Int) = if (x > y) x else y max1: (x: Int, y: Int)Int scala> println(max1(1,10)) 101
2
3
4
5
函数返回值类型可省略(但是最好显示声明,可读性好点), 函数体只有一行可省略大括号
无返回值,无参数的函数
scala> def hello(): Unit = println("hello") hello: ()Unit scala> hello hello scala> hello() hello1
2
3
4
5
6
7
8
scala中无参函数调用可省略小括号。Unit返回值代表返回值为空 (如java中的void)
看下hello函数的最简形式
scala> def hello1 = println("hello1") hello1: Unit scala> hello1 hello11
2
3
4
5
6
4. scala 脚本(script)
scala是支持脚本的。先创建一个hello.scala的文件并追加如下内容:C:\Users\rodbate\Desktop>echo println("hello scala script!") >> hello.scala C:\Users\rodbate\Desktop>scala hello.scala hello scala script!1
2
3
4
5
scala脚本还支持命令行传参数 args(0) 以0开始
C:\Users\rodbate\Desktop>echo println("hello " + args(0)) > args.scala C:\Users\rodbate\Desktop>scala args.scala first second hello first1
2
3
4
5
这一小节就到这了,下节再见。
相关文章推荐
- 从零开始学Scala系列(三)之初识循环分支结构
- 从零开始学Scala系列(三)之初识循环分支结构
- 从零开始学Scala系列(二)之初识Scala
- 从零开始学Scala系列(五)之集合2--Set和Map
- 从零开始学Scala系列(五)之集合2--Set和Map
- 从零开始学Scala系列(四)之数组集合1
- 从零开始学Scala系列(六)之类(Classes)和对象(Objects)
- 从零开始学Scala系列(一)之环境安装及IDE选择安装
- 从零开始学Scala系列(六)之类(Classes)和对象(Objects)
- 从零开始学Scala系列(四)之数组集合1
- 从零开始学Scala系列(一)之环境安装及IDE选择安装
- Bing Maps进阶系列一:初识Bing Maps地图服务
- NSQ系列之nsqlookupd代码分析二(初识nsqlookupd tcpServer)
- 云星数据---Scala实战系列(精品版)】:Scala入门教程004-Scala数组详解003
- OpenGL ES 从零开始系列9a:动画基础和关键帧动画
- 云星数据---Scala实战系列(精品版)】:Scala入门教程045-Scala实战源码-Scala Map操作
- WCF编程系列(一)初识WCF
- 云星数据---Scala实战系列(精品版)】:Scala入门教程055-Scala实战源码-Scala 异常处理
- 从零开始学_JavaScript_系列(22)——dojo(9)(表单、JsonRest的post方法,widget的使用思路)
- 从零开始学.net多线程系列(三)——同步