Scala_Day03
Scala中类的继承和多态
package com.shujia
class Person(id: String, name: String, age: Int) {
val _id: String = id
val _name: String = name
val _age: Int = age
def eat(): Unit = {
println("人类会吃饭。")
}
def run(): Unit = {
println("人类会跑。")
}
}
class Stu(id: String, name: String, age: Int, sId: String) extends Person(id, name, age) {
val _sId: String = sId
override def eat(): Unit = {
println("学生吃了一碗饭。")
}
def learn(): Unit = {
println("学生会学习")
}
}
class Teacher(id: String, name: String, age: Int, tId: String) extends Person(id, name, age) {
val _tId: String = tId
override def eat(): Unit = {
println("老师也吃了一碗饭。")
}
def teach(): Unit = {
println("老师会上课。")
}
}
object Demo10ClassExtends {
def perEatTwice(per:Person):Unit = {
per.eat()
per.eat()
}
def main(args: Array[String]): Unit = {
val zs: Person = new Person("01", "zs", 20)
zs.eat()
zs.run()
val ls: Stu = new Stu("02", "ls", 22, "001")
ls.eat()
val zz: Teacher = new Teacher("03", "zz", 25, "001")
zz.eat()
perEatTwice(zs)
perEatTwice(ls)
perEatTwice(zz)
}
}
样例类
package com.shujia
import scala.io.{BufferedSource, Source}
object Demo11CaseClass {
case class CaseStu(var id: String, name: String, age: Int, gender: String, clazz: String)
def main(args: Array[String]): Unit = {
val zs: CaseStu = new CaseStu("01", "zs", 18, "男", "文科二班")
val zs2: CaseStu = CaseStu("02", "zs2", 18, "男", "文科二班")
println(zs2.id)
zs2.id = "002"
println(zs2.id)
println(zs)
println(zs2)
val bs: BufferedSource = Source
.fromFile("scala/data/stu/students.txt")
bs
.getLines()
.map(line => {
val splits: Array[String] = line.split(",")
CaseStu(splits(0), splits(1), splits(2).toInt, splits(3), splits(4))
})
.filter(_.clazz == "文科二班")
.foreach(stu => println(s"${stu.id},${stu.name},${stu.clazz}"))
bs.close()
}
}
List集合的基本用法
package com.shujia
import scala.io.{BufferedSource, Source}
object Demo12List {
def main(args: Array[String]): Unit = {
val list1: List[Int] = List[Int](1, 2, 2, 2, 2, 3, 4, 5, 6)
val list2: List[Int] = List(1, 2, 3, 4, 5, 6)
println(list1.max)
println(list1.min)
println(list1.size)
println(list1.length)
println(list1.head)
println(list1.tail)
println(list1.isEmpty)
println(list1.nonEmpty)
println(list1.reverse)
println(list1.sum)
println(list1.distinct)
println(list1)
println(list1.indexOf(6))
println(list1(5))
def f(int: Int): Int = {
int * 3
}
val list3: List[Int] = List(1, 2, 3, 4, 5, 6)
println(list3.map(f))
list3.map(f)
.foreach(println)
list3
.map(i => i * 3)
.foreach(println)
list3.foreach(println)
val list4: List[String] = List("Java,Scala,Python", "Hadoop,Hive,HBase", "Spark,Flink,MR")
list4.flatMap(s => s.split(",")).foreach(println)
val list5: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
list5.filter(i => i % 2 == 1).foreach(println)
list5.filter(i => i % 2 == 0).foreach(println)
val bs: BufferedSource = Source
.fromFile("scala/data/stu/students.txt")
val stuList: List[String] = bs
.getLines()
.toList
stuList
.map(line => line.split(",")(4))
.groupBy(clazz => clazz)
.map(kv => s"${kv._1},${kv._2.size}")
.foreach(println)
bs.close()
val list6: List[Int] = List(9, 4, 2, 1, 8, 7, 5, 6)
println(list6.sorted)
list6.sortBy(i => i).foreach(println)
val list7: List[(Int, Int)] = List((1, 7), (3, 5), (2, 6))
list7.sortBy(t2 => t2._1).foreach(println)
list7.sortBy(t2 => t2._2).foreach(println)
list6.sortWith((i1, i2) => i1 < i2).foreach(println)
stuList
.sortWith((stu1, stu2) => {
val clazz01: String = stu1.split(",")(4)
val clazz02: String = stu2.split(",")(4)
val id01: String = stu1.split(",")(0)
val id02: String = stu2.split(",")(0)
val age01: Int = stu1.split(",")(2).toInt
val age02: Int = stu2.split(",")(2).toInt
val flag: Boolean =
if (clazz01 == clazz02) {
if (age01 == age02) {
id01 > id02
} else {
age01 < age02
}
} else {
clazz01 < clazz02
}
flag
})
.foreach(println)
}
}
Tuple的简单介绍
package com.shujia
object Demo13Tuple {
def main(args: Array[String]): Unit = {
val t1: Tuple1[Int] = Tuple1[Int](1)
println(t1._1)
val t4: (Int, Int, Int, Int) = Tuple4[Int, Int, Int, Int](1, 2, 3, 4)
println(t4._1)
println(t4._2)
println(t4._3)
println(t4._4)
val tt4: (Int, Int, Int, Int) = Tuple4(1, 2, 3, 4)
val ttt4: (Int, Int, Int, Int) = (1, 2, 3, 4)
}
}