Scala各种类型实战
来源:互联网 发布:布比网络 编辑:程序博客网 时间:2024/06/05 15:15
- 结构类型
- Infix type
- Self_Type
- Dependency Injection
- Abstract Types
结构类型
def init1( res : { def open() : Unit} ) { res.open } init1(new { def open() = println("Opened")}) type X = {def open() : Unit} init1(new { def open() = println("Opened again")}) object A { def open() { println("A single object Opened")}} init1(A) class Structural { def open() = print("A class instance Opened")} val structural = new Structural init1(structural)
Infix type
object Log {def >>: (data : String) : Log.type = { println(data); Log}} "Hadoop" >>: "Spark" >>: Log val list = List() val newList = "A" :: "B" :: list println(newList) class Infix_type[A, B] val infix : Int Infix_type String = null case class Cons(first : String, second : String) val case_class = Cons("one", "two") case_class match { case "one" Cons "two" => println("Spark!!!")}
Self_Type
class Self{ self => val tmp = "Scala" def foo =self.tmp +this.tmp}trait S1class S2 { this : S1 => }class S3 extends S2 with S1trait T { this : S1 => }object S4 extends T with S1object Self_Type { val c = new S2 with S1}
Dependency Injection
trait Logger { def log (msg : String)}trait Auth { auth : Logger => def act(msg : String){ log(msg) } }object DI extends Auth with Logger{ override def log(msg : String) = println(msg);}object Dependency_Injection { def main(args: Array[String]): Unit = { DI.act("Good luck") }}
Abstract Types
trait Reader{ type In <: java.io.Serializable type Contents def read(in : In) : Contents}class FileReader extends Reader { type In = String type Contents = BufferedSource override def read (name : In) = Source.fromFile(name)}object Abstract_Types { def main(args: Array[String]): Unit = { val fileReader = new FileReader val content = fileReader.read("E:\\Hello.txt") for(line <- content.getLines()) println(line) }}
0 0
- Scala各种类型实战
- 53.Scala中结构类型实战详解
- 54.Scala中复合类型实战详解
- scala学习之: scala的结构类型代码实战
- 云星数据---Scala实战系列(精品版)】:Scala入门教程038-Scala实战源码-Scala match语句03 类型匹配
- Spark经典视频之Scala中复合类型实战详解
- Scala进阶源码实战之六——类型变量
- Scala 类型系统实战之编程进阶(4)
- Scala基础—*类型参数编程实战示例
- Scala深入浅出进阶经典 第53讲:Scala中结构类型实战详解
- Scala深入浅出进阶经典 第54讲:Scala中复合类型实战详解
- scala-43:Scala中类型变量Bounds代码实战及其在Spark中的应用源码解析
- Scala 类型
- Scala类型
- Scala深入浅出进阶经典 第48讲:Scala类型约束代码实战及其在Spark中的应用源码解析
- Scala动手实战入门
- Scala 深入浅出实战经典
- Scala进阶实战
- 【剑指Offer面试题】 九度OJ1513:二进制中1的个数
- hdu2819 swap(二分图匹配)
- Hadoop入门进阶课程3--Hadoop2.X64位环境搭建
- Oracle数据库再次安装
- error LNK2001: unresolved external symbol __imp__SetupDiGetDeviceInterfaceDetailA@24
- Scala各种类型实战
- 如何写好技术简历 —— 实例、模板及工具
- Qt使用的一些总结
- iOS webview 和 js 交互
- Qt4.8.6插件化编程例程--仅供参考
- ios 微博分享
- HTML中引号多层嵌套的问题
- nyoj757 期末考试【队列】
- EJB学习笔记三(有状态和无状态SessionBean的区别)