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

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. Python进阶:metaclass谈

    metaclass 的超越变形特性有什么用? 来看yaml的实例: import yaml class Monster(yaml.YAMLObject): yaml_tag = u'!Monster' ...

  2. 初试牛刀:实时天气WebService

    1.引入WebService:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx 2.声明接口→界面获取值传入接口→接口返回值处理→ ...

  3. CentOS7.9防火墙命令

    CentOS7防火墙命令有变化: CentOS7:   systemctl status firewalld.service     查看防火墙状态 systemctl stop firewalld. ...

  4. 这些方面做好了才能叫运营-App运营日常

    现在APP的开发推广是时代的潮流,同时也是不少企业的难题.现在我们就来谈谈APP运营的一些问题. 1. 基础运营正常维护产品最日常.最普通的工作,如APP应用包在各大应用市场提交上传等,如安卓渠道,包 ...

  5. CSS3浏览器私有属性

    CSS3的浏览器私有属性前缀是一个浏览器生产商经常使用的一种方式.它暗示该CSS属性或规则尚未成为W3C标准的一部分.因此每种内核的浏览器都只能识别带有自身私有前缀的CSS3属性.我们在书写CSS3代 ...

  6. Vue.use()源码分析且执行后干什么了

    直接开始分析源码 // Vue源码文件路径:src/core/global-api/use.js import { toArray } from '../util/index' //initUse函数 ...

  7. 【leetcode】338 .Counting Bits

    原题 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate t ...

  8. pandas 25 式

    英文版 Jupyter Notebook 链接:https://nbviewer.jupyter.org/github/justmarkham/pandas-videos/blob/master/to ...

  9. ubuntu---记录.动态库默认路径的踩坑

    发现这个问题,还是经过一个报错问题卡了好多天,然后请求好多人的支援,最后个人的疑问:为什么明明指明了路径,生成 .SO 没有问题,在调用.SO 就有问题,报错各种找不到函数或者未定义,然后把缺的 *. ...

  10. PHP字符串截取,计算字符串长度

    /** * 字符串截取,支持中文和其他编码 * @param [string] $str [字符串] * @param integer $start [起始位置] * @param integer $ ...