经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Scala » 查看文章
Scala Types 2
来源:cnblogs  作者:afewnotes  时间:2019/11/4 8:46:52  对本文有异议

存在类型

  • 形式: forSome { type ... }forSome { val ... }
  • 主要为了兼容 Java 的通配符
  • 示例

    1. Array[_]
    2. // 等价于
    3. Array[T] forSome { type T}
    4. Map[_, _]
    5. // 等价于
    6. Map[T, U] forSome { type T; type U <: T}

类型系统

类型 语法
Class/Trait class C, trait T
元组 (T1, T2...)
函数 (P1, P2...) => T
注解 T @A
参数类型 A[T1, T2...]
单例类型 value.type
类型投射 O#I
组合类型 T1 with T2 ...
中缀类型 T1 A T2
存在类型 T forSome { type/val... }

以上类型可在编写程序时定义,Scala 也有少量的类型在编译器内部使用

  1. def square(x: Int) = x * x
  2. // REPL 中返回的类型为
  3. // square(x: Int) Int
  4. // 省略的方法定义的 =>

自身类型 self type

  • 形式:this: Type =>
  • 用于限制 trait 只能被混编于指定类型的子类中

    1. trait T1 { def m1()}
    2. trait T2 extends T1 {
    3. this: Super1 with Super2 =>
    4. def m1() { methodInSuper() }
    5. }
    6. // 使用时只能在 Super1,Super2 的子类中混编 with T2
  • 引入的问题:自身类型不会自动继承,必须在子类中重复定义

    1. trait T3 extends T2 {
    2. this: Super1 with Super2 => // 必须重复定义
    3. }

依赖注入

  • 通过 trait 和 自身类型 实现简单的以来注入
    • 需要将所有的依赖都组合起来
    1. trait Logger { def log(msg: String) }
    2. trait Auth {
    3. this: Logger =>
    4. def login(id: String, password: String): Boolean
    5. }
    6. trait App {
    7. this: Logger with Auth =>
    8. // ...
    9. }
    10. object MyApp extends App with FileLogger("test.log") with MockAuth("users.txt")
  • 蛋糕模式 (cake pattern) 实现依赖注入
    • 依赖的组件使用自身类型来表示
    • trait 描述服务接口
    • val 定义需要实例化的服务
    • 层级化组合各个组件,在一个整体中注入需要的组件
    1. // 定义组件1
    2. trait LoggerComponent {
    3. // 描述接口
    4. trait Logger { ... }
    5. // 需要实例化的服务
    6. val logger: Logger
    7. // 接口具体实现
    8. class FileLogger(file: String) extends Logger { ... }
    9. ...
    10. }
    11. // 定义组件2
    12. trait AuthComponent {
    13. // 自身类型限定混编使用的类型
    14. this: LoggerComponent => // Gives access to logger
    15. // 定义服务接口
    16. trait Auth { ... }
    17. // 需要实例化的服务
    18. val auth: Auth
    19. // 接口具体实现
    20. class MockAuth(file: String) extends Auth { ... }
    21. ...
    22. }
    23. // 所有的依赖都集中在一处进行配置/注入
    24. object AppComponents extends LoggerComponent with AuthComponent {
    25. // 实例化服务/注入
    26. val logger = new FileLogger("test.log")
    27. val auth = new MockAuth("users.txt")
    28. }

    Scala编程的蛋糕模式和依赖注入

抽象类型

  • 形式: type Name
  • classtrait 中定义
  • 场景:具体类型需要在子类中确定

    1. trait Reader {
    2. type Contents
    3. def read(fileName: String): Contents
    4. }
    5. // 子类实现是具体确定类型
    6. class StringReader extends Reader {
    7. type Contents = String
    8. def read(fileName: String) = ...
    9. }
    10. class ImageReader extends Reader {
    11. type Contents = BufferedImage
    12. def read(fileName: String) = ...
    13. }
  • 抽象类型、类型参数的使用选择
    • 在类实例化时需要具体确认类型的场景使用类型参数,如 HashMap[String, Int]
    • 期望子类提供具体类型的场景使用抽象类型,如上例中的 Reader

原文链接:http://www.cnblogs.com/yuanzam/p/11779880.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号