learning scala implicit class
隐式类可以用来扩展对象的功能非常方便
example:
- object ImplicitClass_Tutorial extends App {
- println("Step 1: How to define a case class to represent a Donut object")
- case class Donut(name: String, price: Double, productCode: Option[Long] = None)
- println("\nStep 2: How to create instances or objects for the Donut case class")
- val vanillaDonut: Donut = Donut("Vanilla", 1.50)
- println(s"Vanilla donut name = ${vanillaDonut.name}")
- println(s"Vanilla donut price = ${vanillaDonut.price}")
- println(s"Vanilla donut produceCode = ${vanillaDonut.productCode}")
- println("\nStep 3: How to define an implicit class to augment or extend the Donut object with a uuid field")
- object DonutImplicits{
- implicit class AugmentedDonut(donut: Donut) {
- def uuid: String = s"${donut.name} - ${donut.productCode.getOrElse(12345)}"
- }
- }
- println("\nStep 4: How to import and use the implicit class AugmentedDonut from Step 3")
- import DonutImplicits._
- println(s"Vanilla donut uuid = ${vanillaDonut.uuid}")
- }
learning scala implicit class的更多相关文章
- Scala implicit
Scala implicit implicit基本含义 在Scala中有一个关键字是implicit, 之前一直不知道这个货是干什么的,今天整理了一下. 我们先来看一个例子: def display( ...
- learning scala How To Create Implicit Function
println("Step 1: How to create a wrapper String class which will extend the String type") ...
- learning scala akka ask_pattern
package com.example import akka.actor._ import akka.util.Timeout object Tutorial_03_Ask_Pattern exte ...
- learning scala 数组和容器
数组:可变的,可索引的,元素具有相同类型的数据集合 一维数组 scala> val intValueArr = new Array[Int](3)intValueArr: Array[Int] ...
- learning scala control statement
1 .if satement 与其它语言不同的是,scala if statement 返回的是一个值 scala> val a = if ( 6 > 0 ) 1 else -1a: In ...
- learning scala read from file
scala读文件: example: scala> import scala.io.Sourceimport scala.io.Source scala> var inputFile ...
- learning scala write to file
scala 写文件功能: scala> import java.io.PrintWriterimport java.io.PrintWriter scala> val outputFile ...
- learning scala output to console
控制台输出语句: print println example: scala> print("i=");print(i)i=345scala> println(" ...
- learning scala read from console
控制台输入语句: readInt, readDouble, readByte, readShort, readLong, readChar, readBoolean, readLine example ...
随机推荐
- Python进阶:metaclass谈
metaclass 的超越变形特性有什么用? 来看yaml的实例: import yaml class Monster(yaml.YAMLObject): yaml_tag = u'!Monster' ...
- 初试牛刀:实时天气WebService
1.引入WebService:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx 2.声明接口→界面获取值传入接口→接口返回值处理→ ...
- CentOS7.9防火墙命令
CentOS7防火墙命令有变化: CentOS7: systemctl status firewalld.service 查看防火墙状态 systemctl stop firewalld. ...
- 这些方面做好了才能叫运营-App运营日常
现在APP的开发推广是时代的潮流,同时也是不少企业的难题.现在我们就来谈谈APP运营的一些问题. 1. 基础运营正常维护产品最日常.最普通的工作,如APP应用包在各大应用市场提交上传等,如安卓渠道,包 ...
- CSS3浏览器私有属性
CSS3的浏览器私有属性前缀是一个浏览器生产商经常使用的一种方式.它暗示该CSS属性或规则尚未成为W3C标准的一部分.因此每种内核的浏览器都只能识别带有自身私有前缀的CSS3属性.我们在书写CSS3代 ...
- Vue.use()源码分析且执行后干什么了
直接开始分析源码 // Vue源码文件路径:src/core/global-api/use.js import { toArray } from '../util/index' //initUse函数 ...
- 【leetcode】338 .Counting Bits
原题 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate t ...
- pandas 25 式
英文版 Jupyter Notebook 链接:https://nbviewer.jupyter.org/github/justmarkham/pandas-videos/blob/master/to ...
- ubuntu---记录.动态库默认路径的踩坑
发现这个问题,还是经过一个报错问题卡了好多天,然后请求好多人的支援,最后个人的疑问:为什么明明指明了路径,生成 .SO 没有问题,在调用.SO 就有问题,报错各种找不到函数或者未定义,然后把缺的 *. ...
- PHP字符串截取,计算字符串长度
/** * 字符串截取,支持中文和其他编码 * @param [string] $str [字符串] * @param integer $start [起始位置] * @param integer $ ...