美文网首页
《scala编程》第6章

《scala编程》第6章

作者: 富士山下裸奔 | 来源:发表于2018-04-27 22:42 被阅读0次
    love scala
    6.1 Rational类的规格定义
    6.2 构建Rational
    6.3 重新实现toString方法:override 某一个方法
    6.4 检查前置条件require对类的入参进行判断
    6.5 添加字段 val numer :Int = n :创建类的时候,可以在类构造体类实例类, 要知道什么是this, that
    6.6 自引用this关键词this指向当前执行方法的调用对象, 可以省略
    6.7 辅助构造法def this(...): 必须首先调用同一个类的另一个构造法,主构造法是类的单一入口
    CODE

    6.8 私有字段和方法private
    涉及到 初始化器 inititalizers helper method
    The purpose of the private “helper method”gcd is to factor out code needed by some other part of the class, in this case, the primary constructor.

    code
    6.9 自定义操作符

    可以重写+ - 等操作符

    code
    6.10 Scala 中的标识符Identifiers

    alphanumeric identifier 字母数字组合标识符
    Camel-case names of fields, method parameters, local variables,and functions should start with a lower case letter(字段、方法参数、局部变量、函数应该以小写开头),
    EX: ** length, flatMap, and s.
    Camel-case names of classes and traits(类、特质,应该是
    大写字母开头**) should start with an
    upper case letter, EZ: BigInt, List, and UnbalancedTreeMap)
    operator identifier .
    mixed identifier.
    ( defines a unary + operator. Or, **myvar_= **used as method name defines an assignment operator.)
    literal identifier(an arbitrary string enclosed in back ticks (. . .).x <clinit> yield)

    6.11 方法重载

    所谓方法重载methods overloading,就是存在相同方法名的def, 在每个具体的案例中,被选中的是那个最匹配的入参静态类型的重载版本

    code

    6.12 隐式转换
    Implicit conversions作用就是,将某一类转换成另一类(此处是Int整数类转换成Rational有理数类), 目的就是能够让整数类也能使用有理数类里面的方法

    code
    注意事项:
    用操作符作为名称创建方法,以及定义隐式转换有助于设计出调用代码精简并且易于理解的类库,Scala为你提供了强大的能力来设计这样的类库,不过power comes responsibility.

    在设计类库时, 你心中的目标应该不仅仅是让使用代码尽量精简,而是要可读并且可被理解。对于很大的成分来自代码的精简,不过有时候精简也会过度,通过设计那些能够让使用方代码精简得有品位同时又易于理解的类库,可以大幅度提升程序员的工作效率。
    本章的问题:
    Question 1 隐式转换该放在哪?
    Question 2 在类中新建该类,在函数中循环该函数

    相关文章

      网友评论

          本文标题:《scala编程》第6章

          本文链接:https://www.haomeiwen.com/subject/qqbilftx.html