隐式类可以用来扩展对象的功能非常方便

example:

  1. object ImplicitClass_Tutorial extends App {
  2.  
  3. println("Step 1: How to define a case class to represent a Donut object")
  4. case class Donut(name: String, price: Double, productCode: Option[Long] = None)
  5.  
  6. println("\nStep 2: How to create instances or objects for the Donut case class")
  7. val vanillaDonut: Donut = Donut("Vanilla", 1.50)
  8. println(s"Vanilla donut name = ${vanillaDonut.name}")
  9. println(s"Vanilla donut price = ${vanillaDonut.price}")
  10. println(s"Vanilla donut produceCode = ${vanillaDonut.productCode}")
  11.  
  12. println("\nStep 3: How to define an implicit class to augment or extend the Donut object with a uuid field")
  13. object DonutImplicits{
  14. implicit class AugmentedDonut(donut: Donut) {
  15. def uuid: String = s"${donut.name} - ${donut.productCode.getOrElse(12345)}"
  16. }
  17. }
  18.  
  19. println("\nStep 4: How to import and use the implicit class AugmentedDonut from Step 3")
  20. import DonutImplicits._
  21. println(s"Vanilla donut uuid = ${vanillaDonut.uuid}")
  22. }

learning scala implicit class的更多相关文章

  1. Scala implicit

    Scala implicit implicit基本含义 在Scala中有一个关键字是implicit, 之前一直不知道这个货是干什么的,今天整理了一下. 我们先来看一个例子: def display( ...

  2. learning scala How To Create Implicit Function

    println("Step 1: How to create a wrapper String class which will extend the String type") ...

  3. learning scala akka ask_pattern

    package com.example import akka.actor._ import akka.util.Timeout object Tutorial_03_Ask_Pattern exte ...

  4. learning scala 数组和容器

    数组:可变的,可索引的,元素具有相同类型的数据集合 一维数组 scala> val intValueArr = new Array[Int](3)intValueArr: Array[Int] ...

  5. learning scala control statement

    1 .if satement 与其它语言不同的是,scala if statement 返回的是一个值 scala> val a = if ( 6 > 0 ) 1 else -1a: In ...

  6. learning scala read from file

    scala读文件:   example: scala> import scala.io.Sourceimport scala.io.Source scala> var inputFile ...

  7. learning scala write to file

    scala 写文件功能: scala> import java.io.PrintWriterimport java.io.PrintWriter scala> val outputFile ...

  8. learning scala output to console

    控制台输出语句: print println example: scala> print("i=");print(i)i=345scala> println(" ...

  9. learning scala read from console

    控制台输入语句: readInt, readDouble, readByte, readShort, readLong, readChar, readBoolean, readLine example ...

随机推荐

  1. TZOJ5703: C++实验:学生成绩类的实现

    #include<iostream> #include<string> #include<stdio.h> using namespace std; class s ...

  2. Linux上安装pstree命令(-bash: pstree: command not found)

    一.pstree命令的安装 1.在 Mac OS上 brew install pstree 2.在 Fedora/Red Hat/CentOS yum -y install psmisc 3.在 Ub ...

  3. P1777 帮助_NOI导刊2010提高(03)

    也许更好的阅读体验 \(\mathcal{Description}\) Bubu的书架乱成一团了!帮他一下吧! 他的书架上一共有n本书.我们定义混乱值是连续相同高度书本的段数.例如,如果书的高度是30 ...

  4. sqlserver case when 的使用方法

    sql使用case when then 判断某字段是否为null没效果 CASE columnName WHEN null THEN 0 ELSE columnName END虽然columnName ...

  5. 火狐浏览器 访问所有HTTPS网站显示连接不安全解决办法

    当 Firefox 连接到一个安全的网站时(网址最开始为“https://”),它必须确认该网站出具的证书有效且使用足够高的加密强度.如果证书无法通过验证,或加密强度过低,Firefox 会中止连接到 ...

  6. VS.NET(C#)--1.5_VS菜单功能

    VS菜单功能 文件菜单 1.新建 2.添加 编辑菜单 1.快速查找  ctrl+F 2.快速替换   ctrl+H 3.在文件中查找ctrl+shift+F 4.在文件中替换ctrl+shift+H ...

  7. Python之(scikit-learn)机器学习

    一.机器学习(Machine Learning, ML)是一门多领域交叉学科,涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多门学科.专门研究计算机怎样模拟或实现人类的学习行为,以获取新的知识或 ...

  8. VBA switch语句

    当用户想要根据Expression的值执行一组语句时,使用Switch Case语句. 每个值被称为一个”情况”,并根据每种情况变量接通测试.如果测试表达式与用户指定的任何Case不匹配,则执行Cas ...

  9. 了解Scrum敏捷开发过程的优点

    Scrum 1. 我们的团队 1.1 团队名称 开发小分队&7号 1.2 团队成员 徐棒 彭康明 刘鹏芝 罗樟 王小莉 胡广健 沈兴艳 1.3 我在团队中位置 需求实现团队(负责将需求模块实现 ...

  10. cocos creator按钮点击按钮弹起效果设置方法

    如图所示: 只要设置下button的Transition的属性为Scale即可,参数自己调整下.