scala基础-一

scala语言的优势:

scala是一个多范式的编程语言,支持多种编程方式

(1).面向对象编程:封装,继承,多态

(2).函数式编程:优点:代码简介;缺点:可读性太差尤其是隐式类,隐式函数,隐式参数

scala中的数据类型和变量和常量

<1>.scala中任何数据,都是对象,这点和java是不同

<2>.基本数据类型:byte,short,int,long,float,double,string(以上所有都是对象都有对应的方法)

<3>.变量和常量的定义:变量是使用var,常量是使用val(常量的值是不可改变的),可以不指定数据类型,scala可以自动进行类型推到即可以通过后面的值来推导出类型

<4>.Until类型和Nothing类型:Until类型相当于java中的void即函数没有返回值;Nothing类型,一般来说,在执行过程中,产生Exception就是Nothing

函数

<1>.内置函数:scala直接定义的通过导包可以实现的函数
<2>.自定义函数:关键字def

格式:def 函数名称 ([参数名:参数类型]*):返回值类型={}

def add (x:int, y:int):int={x + y}

注意:在scala的函数中是没有return语句的,函数的最后一句话就是函数的返回值,在出现分支的时,每一个分支的最后一句话就是返回值

循环语句

<1>.scala和java中的for,while,dowhile的类似的
<2>.scala中的list等有foreach方法直接遍历
<3>.只有在breakable包中才可以使用break

scala中的函数参数

<1>.call by name:对函数参数求值并且只求一次(参数名称 :=> 参数类型)

<2>.call by value:函数实参在函数体内部用到时才会被求值(参数名称:参数类型)

scala中的参数类型

<1>.默认参数:当没有参数赋值时就使用默认参数值

scala> def fun1(name:String="Tom") : String = "Hello " + name
fun1: (name: String)String

scala> fun1("Andy")
res0: String = Hello Andy

scala> fun1()
res1: String = Hello Tom

<2>.代名参数:当有多个默认参数时,可以使用代名参数来确定给那个参数赋值

eg:当有多个默认参数的时候,通过代名参数可以确定给哪个参数赋值。
scala> def fun2(str:String="Good Morning ",      name:String="Tom ", age:Int=20)=str + name + " and the age is " + age
fun2: (str: String, name: String, age: Int)String

scala> fun2()
res2: String = Good Morning Tom  and the age is 20

scala> fun2(name="Mary ")
res3: String = Good Morning Mary  and the age is 20

<3>.可变参数:和java中的可变参数类型一直即参数数量不固定

求多个数字的和:
def sum(args:Int*) = {
var result = 0
for(s <- args) result += s
result
}

scala> def sum(args:Int*) = {
| var result = 0
| for(s <- args) result += s
| result}
sum: (args: Int*)Int

scala> sum(1,2,4)
res4: Int = 7

scala> sum(1,2,4,3,2,5,3)
res5: Int = 20

lazy值

<1>.如果常量是lazy的话,它的初始化就会被延迟,推迟到第一次使用该常量的时候

eg:scala> val x : Int = 10
x: Int = 10

scala> val y:Int = x+1
y: Int = 11

y 不是 lazy,定义后立即触发计算

scala> lazy val z : Int = x+1
z: Int = <lazy>

z 是lazy,初始化会被推迟,定义时不会出发计算。

scala> z
res6: Int = 11

当我们第一次使用z的时候,才会触发计算。

多余:

spark的核心就是算子(RDD数据集合),Spark提供了多种方法来操作RDD.

算子分为两种:
1.Transformation:延时加载,不会触发计算
2.Action:会触发计算

eg:(1)读一个存在的文件

scala> lazy val words = scala.io.Source.fromFile("H:\\tmp_files\\student.txt").mkString
words: String = <lazy>

scala>  words
res7: String =
1       Tom     12
2       Mary    13
3       Lily    15

(2)读一个不存在的文件
scala> val words = scala.io.Source.fromFile("H:\\tmp_files\\studen1231312312t.txt").mkString
java.io.FileNotFoundException: H:\tmp_files\studen1231312312t.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at scala.io.Source$.fromFile(Source.scala:91)
at scala.io.Source$.fromFile(Source.scala:76)
at scala.io.Source$.fromFile(Source.scala:54)
... 32 elided
会产生异常

scala> lazy val words = scala.io.Source.fromFile("H:\\tmp_files\\studen1231312312312t.txt").mkString
words: String = <lazy>
不会产生异常

posted @ 2020-04-07 17:11  情丶书生  阅读(163)  评论(0编辑  收藏  举报