原文链接 http://stackoverflow.com/questions/16348541/scala-example-use-for-early-definition-early-initializer-pre-initialized-fi#


 

Let's see a example from the Programming in Scala book (page 451). If we have a definition like this:

trait RationalTrait { val numerArg: Int val denomArg: Int }

Then numerArg and denomArg are called abstract vals & the trait can be used directly without extends, like this:

val x = new RationalTrait { val numerArg = 1 val denomArg = 2 }

Or

val y = new { val numerArg = 1 val denomArg = 1 } with RationalTrait

The above two are both valid Pre-initializing of abstract val in trait, except that when you need to put an expression value to abstract vals, you can only use the later form, like this:

val z = new { val numerArg = 1 * x val denomArg = 2 * x } with RationalTrait

Another interesting example in book is to Pre-initialized fields in a class definition.

class RationalClass(n: Int, d: Int) extends { val numerArg = n val denomArg = d } with RationalTrait { def + (that: RationalClass) = new RationalClass( numer * that.denom + that.numer * denom, denom * that.denom ) }

转载于:https://www.cnblogs.com/ihongyan/p/4712298.html

【转】Scala: Example use for early definition / early initializer / pre-initialized fields相关推荐

  1. 35_pytorch 过拟合解决办法 (Early Stop, Dropout)

    关于"深度学习过拟合解决方案": https://blog.csdn.net/tototuzuoquan/article/details/113802684?spm=1001.20 ...

  2. early翻译_early是什么意思_early翻译_读音_用法_翻译

    vipkid提供early是什么意思.early英语单词推荐.early英语解释.early相关词.early英语短语 英音 [ˈɜ:li] 美音 [ˈɜrli] [中文释义] adj. 早的;早期的 ...

  3. A Scala Tutorial for Java Programmers

    A Scala Tutorial for Java Programmers English Español 한국어 Deutsch By Michel Schinz and Philipp Halle ...

  4. scala特质_Scala的特质

    scala特质 Scala特质 (Scala traits) Traits in Scala are like interfaces in Java. A trait can have fields ...

  5. 创建一个带有Event Receiver的List Definition

    文章中介绍了详细的步骤, 还有一个视频可供下载. 文章地址: Building List Definitions with Event Receiver in Windows SharePoint S ...

  6. enterpriseTECH Dec 10

    enterpriseTECH Dec 10 文章目录 enterpriseTECH Dec 10 **Business models and value propositions** understa ...

  7. 4E4 models

    4E4 models 文章目录 4E4 models L1 L2 Future Planning (Roadmapping) Strategic foresight Industrial emerge ...

  8. Python自然语言处理学习笔记(68):7.9 练习

    7.9   Exercises  练习 ☼ The IOB format categorizes tagged tokens as I, O and B. Why are three tags nec ...

  9. Linux内核深入理解中断和异常(5):外部中断

    Linux内核深入理解中断和异常(5):外部中断 rtoax 2021年3月 1. 外部中断简介 外部中断包括:键盘,鼠标,打印机等. 外部中断包括: I/O interrupts; IO中断 Tim ...

最新文章

  1. mobaxterm设置中文界面显示_Adobe系列2020如何更改界面显示语言
  2. 计算机组成原理—— 寻址方式
  3. sqlconfigdatasource mysql_MFC odbc 连接MySQL 的 (SQLConfigDataSource动态DSN)
  4. mysql 主从备份 主服务器配置_同一服务器配置Mysql主从备份
  5. 解决ZF2_PATH environment
  6. python中if控制语句_Python 极简教程(十二)逻辑控制语句 if else
  7. PHPExcel用法有感
  8. 5G 和 IoT 究竟意味着什么?
  9. server.xml解析
  10. MyReport.TD套打引擎介绍
  11. linux下游戏制作工具,在Linux下可用Wine安装和运行D5Power游戏制作工具、蜂窝助手...
  12. Google play谷歌应用商店 APP上包上架的一些策略和技巧
  13. 面试官如何对应聘者的素质与能力做出相对准确的判断
  14. win10双显卡怎么切换amd和英特尔_手把手给你细说win10系统双显卡设置只使用独立显卡的流程...
  15. 大数据开发和java的前景_大数据开发和软件开发哪个前景好?
  16. 拼多多推广没有曝光怎么办?
  17. 公众平台 php,微信公众平台PHP开发(转载)
  18. 计算机监控系统维护重点,计算机监控系统维护规程
  19. es 聚合查询 java api_javaAPI操作ES分组聚合
  20. (转)周鸿祎产品秘笈:小版本成就大产品

热门文章

  1. python中if函数的使用方法_(for i in range) (else) (if)使用方法
  2. 股票价格跨度--单调栈
  3. delphi文本转换mp3_基于百度AI 的语音合成、转换的 Yii2 sdk.
  4. wkwebview 不加载ajax,WKWebView加载显示问题
  5. 查看oracle大页,在Oracle11.2.0.3.0上开启大页(hugepages)的详细解析
  6. matlab2012b函数问题,MATLAB版本(2012b 64bit)在尝试调用svmtrain函数时报错
  7. 1909升级卡64_【春节配置推荐】第3期:设计娱乐万元配置推荐、四代升级建议参考...
  8. python支付程序源码_支付宝推出新活动,Python脚本能让你赚的更多!(附源码)...
  9. linux删除文件里夹命令,Linux删除文件(夹)、创建文件(夹)命令是什么
  10. jq 获取引入页面url_jqURL获取页面URL及参数