控制结构在spark源码中的鉴赏
none 省写的话默认是()是返回any类型,而none是optional类型。
case object None extends Option[Nothing] {
def isEmpty = true
def get = throw new NoSuchElementException("None.get")
}
package com.dt.spark.scala.bascis
object ControlStructures {
def main(args: Array[String]): Unit = {
println("scala")
var age =30
//if else 有值的,而java中if没有值的
println (if (age >25 ) "Worker" else "student")
val result = if (age >25 ) "Worker" else "student"
println(result)
//val result2: Any 是 字符串和整型的父类any
// if条件表达式进行类型推断
val result2 = if (age >18 ) "adult " else 1
println(result2)
//val result3: Any
val result3 = if (age >58 ) "adult "
println(result3 )
//val result33: Any result3全写的写法
//默认 ()空值表示 unit
val result33 = if (age >48 ) "adult " else ()
println(result33)
val result4 = if (age >48 ) "adult " else None
}
}
运行结果
scala
Worker
Worker
adult
()
()
object ControlStructures {
def main(args: Array[String]): Unit = {
println("scala")
var age = 10
//if else 有值的,而java中if没有值的
println(if (age > 25) "Worker" else "student")
val result = if (age > 25) "Worker" else "student"
println(result)
//val result2: Any 是 字符串和整型的父类any
// if条件表达式进行类型推断
val result2 = if (age > 18) "adult " else 1
println(result2)
//val result3: Any
val result3 = if (age > 58) "adult "
println(result3)
//val result33: Any result3全写的写法
//默认 ()空值表示 unit
val result33 = if (age > 48) "adult " else ()
println(result33)
val result4 = if (age > 48) "adult " else None
var x, y = 0
val result5 = if (age < 18) {
x = x + 1
y = y + 1
x + y
} else 0
println(result5)
println("==================")
for (i <- 0 to 5 if i == 2) {
println(i)
}
var flag =true
var sum=0
for (i <- 0 to 6 if flag) {
sum = sum + i
if (5 == i ) flag =false
}
println("sum: "+sum)
for (item <-"hello spak") println(item)
for (item <-"hello spak".split(" ")) println(item)
}
}
scala
student
student
1
()
()
2
==================
2
sum: 15
h
e
l
l
o
s
p
a
k
hello
spak
for循环中能够提取出什么内容取决于后面集合的类型!!!
object ControlStructures {
def main(args: Array[String]): Unit = {
println("scala")
var age = 10
//if else 有值的,而java中if没有值的
println(if (age > 25) "Worker" else "student")
val result = if (age > 25) "Worker" else "student"
println(result)
//val result2: Any 是 字符串和整型的父类any
// if条件表达式进行类型推断
val result2 = if (age > 18) "adult " else 1
println(result2)
//val result3: Any
val result3 = if (age > 58) "adult "
println(result3)
//val result33: Any result3全写的写法
//默认 ()空值表示 unit
val result33 = if (age > 48) "adult " else ()
println(result33)
val result4 = if (age > 48) "adult " else None
var x, y = 0
val result5 = if (age < 18) {
x = x + 1
y = y + 1
x + y
} else 0
println(result5)
println("==================")
for (i <- 0 to 5 if i == 2) {
println(i)
}
var flag =true
var sum=0
for (i <- 0 to 6 if flag) {
sum = sum + i
if (5 == i ) flag =false
}
println("sum: "+sum)
sum=0
for (i <- 0 to 6 ) {
sum = sum + i
// if (5 == i ) return // return 返回的是方法级别的,后面也不打印了
}
println("sum return : "+sum)
for (item <-"hello spak") println(item)
for (item <-"hello spak".split(" ")) println(item)
// for 循环用守卫
//while 用flag
import scala.util.control.Breaks._
flag =true
breakable {
while ( flag ) {
for (item <- "spark") {
println ("while item======="+item)
if (item == 'r') {
flag = false
break
}
}
}
}
println ("over")
}
}