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 ...
随机推荐
- TZOJ5703: C++实验:学生成绩类的实现
#include<iostream> #include<string> #include<stdio.h> using namespace std; class s ...
- 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 ...
- P1777 帮助_NOI导刊2010提高(03)
也许更好的阅读体验 \(\mathcal{Description}\) Bubu的书架乱成一团了!帮他一下吧! 他的书架上一共有n本书.我们定义混乱值是连续相同高度书本的段数.例如,如果书的高度是30 ...
- sqlserver case when 的使用方法
sql使用case when then 判断某字段是否为null没效果 CASE columnName WHEN null THEN 0 ELSE columnName END虽然columnName ...
- 火狐浏览器 访问所有HTTPS网站显示连接不安全解决办法
当 Firefox 连接到一个安全的网站时(网址最开始为“https://”),它必须确认该网站出具的证书有效且使用足够高的加密强度.如果证书无法通过验证,或加密强度过低,Firefox 会中止连接到 ...
- VS.NET(C#)--1.5_VS菜单功能
VS菜单功能 文件菜单 1.新建 2.添加 编辑菜单 1.快速查找 ctrl+F 2.快速替换 ctrl+H 3.在文件中查找ctrl+shift+F 4.在文件中替换ctrl+shift+H ...
- Python之(scikit-learn)机器学习
一.机器学习(Machine Learning, ML)是一门多领域交叉学科,涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多门学科.专门研究计算机怎样模拟或实现人类的学习行为,以获取新的知识或 ...
- VBA switch语句
当用户想要根据Expression的值执行一组语句时,使用Switch Case语句. 每个值被称为一个”情况”,并根据每种情况变量接通测试.如果测试表达式与用户指定的任何Case不匹配,则执行Cas ...
- 了解Scrum敏捷开发过程的优点
Scrum 1. 我们的团队 1.1 团队名称 开发小分队&7号 1.2 团队成员 徐棒 彭康明 刘鹏芝 罗樟 王小莉 胡广健 沈兴艳 1.3 我在团队中位置 需求实现团队(负责将需求模块实现 ...
- cocos creator按钮点击按钮弹起效果设置方法
如图所示: 只要设置下button的Transition的属性为Scale即可,参数自己调整下.