Scala面向对象编程
1.课程目标
- 掌握Scala中面向对象编程
- 掌握Scala模式匹配
- 掌握Scala泛型高级内容
- 熟悉Scala中多线程编程模型
2.Scala 类
3.scala中setter getter
- Scala中方法调用原则
5.Scala中构造函数
第一种: 主构造器
定义格式: class 类名(参数列表*)
第二种: 辅助构造器 定义在类的内部
定义格式:
def this(参数列表){
//第一行代码必须是调用主构造器
this(参数列表) //class 类名(参数列表*)
}
package cn.itcast.day02
/**
* Animal
*/
class Animal(val name: String) {
var color: String = _
var legs: Int = _
//创建辅助构造器
def this(name: String,color: String,legs: Int) {
this(name)
this.color = color
this.legs = legs
}
//创建多个辅助构造器
def this() {
this("itcast")
}
}
object Scala_day02_constructor {
def main(args: Array[String]): Unit = {
val animal = new Animal() //辅助构造器
val cat = new Animal("cat") //调用主构造器
val cat2 = new Animal("cat","yellow",4) //辅助构造器
println(cat)
println(cat2)
println(animal)
println(animal.name)
}
}
注意: 主构造器有且仅有一个
辅助构造器可以具有多个
主构造器中参数 如果有val 修饰的话 public 如果有没有 private
- scala中单例对象
7.伴生对象和伴生类
- main函数
9.scala中继承关系
10.scala构造机制
11.子类调用父类有参的构造函数
第一种情况:
子类构造函数无参 父类的构造函数有参数
第二种情况:
子类构造函数有参数 父类构造函数也有参数
class PC(val memory: Int,val cpu: Int)
//子类
class NotePC extends PC(memory = 100,cpu = 100)
class DeskPC(memory: Int,cpu: Int) extends PC(memory,cpu)
object Scala_day02_parent {
def main(args: Array[String]): Unit = {
val c = new NotePC
println(c.memory)
println(c.cpu)
val dc = new DeskPC(120,130)
println(dc.memory)
println(dc.cpu)
}
}
12.抽象类
13.匿名内部类
14.强制类型转换
- 超类
16.特质
17.模式匹配
18.偏函数
偏函数:函数体 或者方法体 是一个模式匹配的形式
def 名称(参数) = 参数 match {
case 匹配
}
val 名称: PartialFunction[输入数据类型,输出数据类型] ={
case 匹配
}
/**
* def 名称(参数) =参数 match {
* case 匹配
* }
*/
def getInt(x: Int): Int = x match {
case 10 => 10 * 10
case 20 => 20 * 20 * 20
case _ => 1000
}
def main(args: Array[String]): Unit = {
val i = getInt(30)
println(i)
}
val fun: PartialFunction[Int,String] = {
case 10 => String.valueOf(10 * 10)
case 20 => String.valueOf(20 * 20 * 20)
case _ => String.valueOf(1000)
}
val str = fun(20)
println(str)
19.泛型的逆变协变和非变
20.方法参数的上下界操作
通过规定方法参数的上下界操作,可以规定方法参数的传入过程
U>:A : 下界操作 U是A类本身或者其父类
S<:A : 上界操作 S是A类本身或者其子类
class Man
class OldMan extends Man
class OOldMan extends OldMan
class YoungMan extends Man
class YYoungMan extends YoungMan
class Sport {
//适合老年人方法 规定方法下界 U 是oldMan的本身或者是其父类
def taiji[U >: OOldMan](man: U): Unit = {
println("打太极")
}
//适合年轻人 规定上界 S:代表的是YoungMan本身 或者其子类
def bengji[S <: YoungMan](youngMan: S): Unit = {
println("年轻人游戏")
}
}
object Scala_day02_upanddown {
def main(args: Array[String]): Unit = {
/* val yman = new YoungMan
val yyman = new YYoungMan
val sport = new Sport
sport.bengji(yyman)*/
val sport = new Sport
val man = new Man
val yyman = new YYoungMan
val oldMan = new OldMan
//val ooldMan = new OOldMan
sport.taiji(yyman)
}
}
class SuperBox[+T,-S] {
//上界方法
def up[P <: S](s: S): Unit = {
}
def down[D >: T](d: D): Unit = {
}
}
21.Scala中并发编程
22.基于Actor的wordcount案例
- 需求:用 actor 并发编程写一个单机版的 WordCount,将多个文件作为输入,计算完成后将多 个任务汇总,得 到终的结果
- 实现步骤:
- 编写读取文件,并统计文件wordcount的工具方法
- 实现actor 并通过loop react方式接受消息。利用 case class 样例类去匹配对应的操作 并将结果封装在样 例类中,通过sender返回消息
- 主线程汇总返回结果,并将结果保存在ListBu?er中
- 后对ListBu?er中数据进行全局汇总
- 汇总完成后,关闭所有线程
package cn.itcast.day02
import java.io.File
import scala.actors.{Actor,Future}
import scala.collection.mutable.ListBuffer
import scala.io.Source
/** 需求:3个文件
* 启动三个Actor 读取3个文件
* 对这个三个文件中数据 word 进行统计
* 打印统计结果
* */
case class Task(filePath: String)
case class ReplayMap(map: Map[String,Int])
class MyActor28 extends Actor {
override def act(): Unit = {
loop {
react { case Task(filePath) => {
val lines = Source.fromFile(new File(filePath)).getLines().toArray
val groupByResult = lines.flatMap(x => x.split(",")).map(w => (w,1)).groupBy(t => t._1)
val result = groupByResult.mapValues(a => a.length)
sender ! ReplayMap(result)
}
}
}
}
}
object Scala_28_Actor {
def main(args: Array[String]): Unit = {
val arr = Array("D://a.txt","D://b.txt","D://c.txt")
//装返回数据
val list = new ListBuffer[Future[Any]]()
val listReplayMap = new ListBuffer[ReplayMap]()
for (filePath <- arr) {
val a = new MyActor28()
a.start()
val result = a !! Task(filePath)
list += result
}
//循环遍历接受数据的操作
while (list.size > 0) {
val flterReulst = list.filter(f => f.isSet)
for (f <- flterReulst) {
listReplayMap += f.apply().asInstanceOf[ReplayMap]
list -= f
}
}
val groupByListBuffer = listReplayMap.map(rm => rm.map).flatten.groupBy(t => t._1)
//lb 是listBuffer (tuple)
val wordcount = groupByListBuffer
.mapValues(lb => {
//t 代表 tuple
// map(t=>t._2) 拿到tuple的value数据
// x:初始值 0 y 第一个元素
lb.map(t => t._2).reduce((x,y) => x + y)
})
for (wc <- wordcount) {
println(wc)
}
}
}
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|