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 ...
随机推荐
- Django REST framework 基本组件
一.序列化组件 简单使用 开发我们的Web API的第一件事是为我们的Web API提供一种将代码片段实例序列化和反序列化为诸如json之类的表示形式的方式.我们可以通过声明与Django forms ...
- Python的json操作
对数据: json = json.dumps(data) 编码 dict->string 排序sort_keys=True, 缩进indent=4, 分隔符separators=(' ...
- docker 实践六:dockerfile 详解
本篇开始来学习关于 dockerfile 的知识. 注:环境为 CentOS7,docker 19.03. dockerfile 是⼀个⽂本格式的配置⽂件, ⽤户可以使⽤ dockerfile 来快速 ...
- PB计算两个日期相差月份(计算工龄)
ll_intime_y = year(date(this.object.in_factory_day[row])) ll_intime_m = month(date(this.object.in_fa ...
- Linux下 sftp服务配置
查看openssh的版本,使用ssh -V 命令来查看openssh的版本,版本必须大于4.8p1,低于的这个版本需要升级. 参考博客:https://yq.aliyun.com/articles/6 ...
- hdu 2539 虽然是水题 wa了很多次 说明自己的基本功不扎实 需要打好基础先 少年
两点吧 1.gets使用的时候 确保上一次的回车符对其没有影响 getline也是如此 这样的细节.. 多注意啊!! 2.编写程序的时候 对一些极端的情况要多调试 比如此题当 n==1的时候.. ...
- C#在txt类文件中追加内容
string path = "test.txt"; FileStream mystream = new FileStream(path, FileMode.OpenOrCreate ...
- 【转载】Asp.Net中Cookie对象的作用以及常见属性
Cookie对象是服务器为用户访问存储的特定信息,这些信息一般存储在浏览器中,服务器可以从提交的数据中获取到相应的Cookie信息,Cookie的最大用途在于服务器对用户身份的确认,即票据认证,用户会 ...
- Html CSS transform matrix3d 3D转场特效
Html CSS transform matrix3d 3D转场特效 透视矩阵 2n/(r-l) 0 (r+l)/(r-l) 0 0 2n/(t-b) (t+b)/(t-b) 0 0 0 (n+f)/ ...
- js控制台不同的打印方式
在控制台单个输出: console.log(...):值 console.info(...):信息 console.debug(...):调试信息 console.warn(...):警告信息 con ...