// 字符串模板(插值字符串): 通过$获取变量值 println(s"${age}岁的${name}在学习") val num: Double = 2.3456 println(f"The num is ${num}%2.2f") // 格式化模板字符串 println(raw"The num is ${num}%2.2f")
// 三引号表示字符串,保持多行字符串的原格式输出 val sql = s""" |select * |from | student | where | name = ${name} | and | age > ${age} |""".stripMargin println(sql)
控制台输入
1 2 3 4 5
// 输入信息 println("请输入大名:") val name = StdIn.readLine() println("请输入年龄:") StdIn.readInt()
val age = 4 val result = if (age < 10){ println("儿童") } println(result)
val age2 = 4 val result2: String = if (age2 < 10){ println("儿童") "儿童" }else { "成年" } println(result2)
注意:条件分支的最后一行为返回值
循环
范围遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// 范围遍历 for (i <- 1 to 10) { // 前后闭合 println(i) } println("=========================") for (i <- 1.to(10)) { println(i) } println("=========================") for (i <- Range(1, 10)){ // 不包含右边界 println(i) } println("=========================") for (i <- 1 until 10 ){ // 不包含右边界 println(i) } for (i <- 10 to 1 reverse) { println(i) }
集合遍历
1 2 3 4 5 6 7 8 9 10
// 集合遍历 for (i <- Array(12, 34, 53) ){ println(i) } for (i <- List(12, 34, 53) ){ println(i) } for (i <- Set(12, 34, 53) ){ println(i) }
循环守卫
1 2 3
for (i <- 1 to 10if i != 5) { println(i) }
循环步长
1 2 3 4 5 6
for (i <- 1 to 10 by 2) { println(i) } for (i <- 30 to 13 by -2) { println(i) }
嵌套循环
1 2 3 4 5 6 7
for (i <- 1 to 3; j <- 1 to 3) { // 中间分号分割 println(i, j) } for (i <- 1 to 3; j <- 1 to 3if j <= i) { print(i, j) if (j == i) println() }
引入变量
1 2 3 4 5 6 7 8 9 10 11
for (i <- 1 to 3; j = 4 - i) { println(i, j) } for { i <- 1 to 3; j = 4 - i } { println(i, j) } for (i <- 1 to 9 ; stars = 2 * i - 1; spaces = 9 - i ){ // 打印等腰三角形 println(" " * spaces + "*" * stars) }
循环返回
1 2 3 4 5 6 7 8
// 循环返回值 val a: Unit = for (i <- 1 to 10){ i } println("a = " + a) // a = ()
val b = for (i <- 1 to 10) yield i println("b = " + b) // b = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
// 循环中断 (采用抛出异常方式) try { for (i <- 0 to 4) { if (i == 3) thrownew RuntimeException println(i) } }catch { case e: Exception => // 什么都不做,只是退出循环 } println("这是循环外的代码")
// 使用Scala中的breaks类的break方法, 实现异常的抛出和捕捉 Breaks.breakable( for (i <- 0 to 4) { if (i == 3) Breaks.break() println(i) } ) println("这是循环外的代码")
// 使用Scala中的breaks类的break方法, 实现异常的抛出和捕捉(省略Breaks) breakable( for (i <- 0 to 4) { if (i == 3) break() println(i) } ) println("这是循环外的代码")
def main(args: Array[String]): Unit = { val student1 = new Student1 // a, 主构造方法被调用 student1.Student1() // 一般方法被调用 val student2 = new Student1("chen") val student3 = new Student1("chen02", 10) }
打印结果 a, 主构造方法被调用 一般方法被调用 a, 主构造方法被调用 b, 辅助构造方法一被调用 name:chen age: 0 a, 主构造方法被调用 b, 辅助构造方法一被调用 name:chen02 age: 0 c, 辅助构造方法二被调用 name:chen02 age: 10
object Obj11 { def main(args: Array[String]): Unit = { val student1 = Student11.newStudent("chen", 12) student1.printInfo() } }
// 定义类 class Student11 private (val name: String, val age: Int){ def printInfo(): Unit ={ println(s"student name = $name age = $age school = ${Student11.school}") } }
// 伴生对象 object Student11{ val school: String = "xue"
object Obj11 { def main(args: Array[String]): Unit = { val student2 = Student11.apply("chen", 12) val student3 = Student11("chen", 12) student1.printInfo() student2.printInfo() } }
// 定义类 class Student11 private (val name: String, val age: Int){ def printInfo(): Unit ={ println(s"student name = $name age = $age school = ${Student11.school}") } }
// 伴生对象 object Student11{ val school: String = "xue"
object Obj_trait13 { def main(args: Array[String]): Unit = { val student1 = new Student13 student1.sayHello() } // 打印结果 // hello from: student // student hello from: student }
// 父类 classPerson13{ val name: String = "person" var age: Int = 18 def sayHello(): Unit = { println("hello from: " + name) } }
// 定义特质 trait Young { // 声明抽象和非抽象属性 var age:Int val name: String = "yount"
// 声明抽象和非抽象方法 def play(): Unit = { println("young perple is playing") } def dating(): Unit } class Student13 extends Person13 with Young { // 重写冲突的属性 override val name: String = "student" // 实现抽象方法 def dating(): Unit = println(s"student $name dating")
// def study(): Unit = println(s"student $name is studying")
object Obj_trait15 { def main(args: Array[String]): Unit = { val myFootBall = new MyFootBall println(myFootBall.describe()) // my ball is red-foot-ball } }
object Obj_extend17 { def main(args: Array[String]): Unit = { // 类型的检测和转换 val student1 = new Student17("chen", 18) student1.study() // student study student1.sayHello() // hi form student chen val person: Person17 = new Student17("chen", 18) person.sayHello() // hi form student chen
// 类型判断 println("student is student17: " + student1.isInstanceOf[Student17]) // student is student17: true println("student is person17: " + student1.isInstanceOf[Person17]) // student is person17: true println("person is person17: " + person.isInstanceOf[Person17]) //person is person17: true println("person is student17: " + person.isInstanceOf[Student17]) // person is student17: true val person2: Person17 = new Person17("chen", 18) println("person2 is person17: " + person2.isInstanceOf[Person17]) //person2 is person17: true println("person2 is student17: " + person2.isInstanceOf[Student17]) // person2 is student17: false
// 类型转换 if (person.isInstanceOf[Student17]) { val newStudent = person.asInstanceOf[Student17] newStudent.study() // student study }
println(classOf[Student17]) // class chapter07.Student17 } }
class Person17(val name: String, val age: Int){ def sayHello(): Unit = { println("hi form person " + name ) } }
class Student17( name: String, age: Int) extends Person17 (name, age){ override def sayHello(): Unit = { println("hi form student " + name ) }
def study(): Unit = { println("student study") } }