Scala学习笔记(七):Application特质
Trait 的一个主要用法,将一个瘦接口变成胖接口
trait Philosophical{
def philosophize(){
println("here")
}
}
class Forg extends Philosophical{
override def toString="green"
}
val forg=new Forg
forg.philosophize
typeclass模式
trait Tellable[T] {
def tell(t: T): String
}
case class Color(descript: String)
case class Person(name: String)
object colorTeller extends Tellable[Color] {
def tell(t: Color): String = "I am color "+t.descript
}
val personTeller = new Tellable[Person] {
def tell(t: Person): String = "I am "+ t.name
}
def tell[T](t: T)(M: Tellable[T]) = {
M.tell(t)
}
应用:
tell(Color("RED"))(colorTeller)
tell(Person("John"))(personTeller)
应用implicit
案例一:
object TestTrait {
trait Tellable[T] {
def tell(t: T): String
}
case class Color(descript: String)
implicit object colorTeller extends Tellable[Color] {
def tell(t: Color): String = "I am color "+t.descript
}
def main(args: Array[String]): Unit = {
import colorTeller._
println(tell(Color("RED")))
}
}
案例二:
trait Monoid[A] {
def mappend(a1: A, a2: A): A
def mzero: A
}
implicit object intMonoid extends Monoid[Int] {
def mappend(i1: Int, i2: Int): Int = i1 + i2
def mzero = 0
}
implicit object stringMonoid extends Monoid[String] {
def mappend(s1: String, s2: String): String = s1 + s2
def mzero = ""
}
def sum[A](xs: List[A])(implicit m: Monoid[A]): A = xs.foldLeft(m.mzero)(m.mappend)
//> sum: [A](xs: List[A])(implicit m: scalaz.learn.ex2.Monoid[A])A
sum(List(1, 2, 3)) //> res0: Int = 6
sum(List("Hello,", " how are you")) //> res1: String = Hello, how are you
Application Trait
Scala提供了特质scala.Application,在单例对象名后面写上“extends Application”,把想要执行的代码直接放在单例对象的花括号之间
import ChecksumAccumulator.calculate
object FallWinterSpringSummer extends Application {
for (season <- List("fall", "winter", "spring"))
println(season +": "+ calculate(season))
}
能这么做,是因为特质Application声明了带有合适签名的main方法,并被你写的单例对象继承
花括号之间的代码被收集进了单例对象的主构造器,并在类初始化时执行
优点:
1.可以减少一些输入工作
缺点:
1.不能访问命令行参数
Ordered Trait
Scala学习笔记(七):Application特质的更多相关文章
- [Scala]Scala学习笔记七 正则表达式
1. Regex对象 我们可以使用scala.util.matching.Regex类使用正则表达式.要构造一个Regex对象,使用String类的r方法即可: val numPattern = &q ...
- scala 学习笔记七 基于类型的模式匹配
1.介绍 Scala 提供了强大的模式匹配机制,应用也非常广泛. 一个模式匹配包含了一系列备选项,每个都开始于关键字 case.每个备选项都包含了一个模式及一到多个表达式.箭头符号 => 隔开了 ...
- 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性
基于.net的分布式系统限流组件 在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...
- go微服务框架kratos学习笔记七(kratos warden 负载均衡 balancer)
目录 go微服务框架kratos学习笔记七(kratos warden 负载均衡 balancer) demo demo server demo client 池 dao service p2c ro ...
- (转)Qt Model/View 学习笔记 (七)——Delegate类
Qt Model/View 学习笔记 (七) Delegate 类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...
- Learning ROS for Robotics Programming Second Edition学习笔记(七) indigo PCL xtion pro live
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS forRobotics Pro ...
- Self-Host c#学习笔记之Application.DoEvents应用 不用IIS也能執行ASP.NET Web API
Self-Host 寄宿Web API 不一定需要IIS 的支持,我们可以采用Self Host 的方式使用任意类型的应用程序(控制台.Windows Forms 应用.WPF 应用甚至是Wind ...
- Typescript 学习笔记七:泛型
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- python3.4学习笔记(七) 学习网站博客推荐
python3.4学习笔记(七) 学习网站博客推荐 深入 Python 3http://sebug.net/paper/books/dive-into-python3/<深入 Python 3& ...
- Go语言学习笔记七: 函数
Go语言学习笔记七: 函数 Go语言有函数还有方法,神奇不.这有点像python了. 函数定义 func function_name( [parameter list] ) [return_types ...
随机推荐
- Ubuntu Server安装图形界面全过程
转载自:http://blog.csdn.net/sunbaigui/article/details/6624110, http://mikewolfli.blog.163.com/blog/stat ...
- loop指令
loop系列的指令有:loop,loope/loopz,loopne/loopnz,它们都是借助于ECX寄存器作为计数来实现循环,每轮循环先ecx自动减1,再来判断ecx值,ecx的自减不会影响OF和 ...
- SSH web.xml文件配置
启动一个WEB项目的时候, WEB容器会去读取它的配置文件web.xml web.xml中配置的加载优先级:context-param -> listener -> filter -> ...
- [转] KVM Internals, code and more
KVM Kernel-based Virtual Machine Internals, code and more http://slides.com/braoru/kvm#/ What behind ...
- [转]Oracle如何实现创建数据库、备份数据库及数据导出导入的一条龙操作
本文转自:http://www.cnblogs.com/wuhuacong/archive/2012/03/09/2387680.html Oracle中对数据对象和数据的管理,无疑都是使用PL/SQ ...
- 【OpenCV】全景拼接
从OpenCV3.0正式版开始,features2d中的一些接口,搬到附加库xfeatures2d中了,其中就有SIFT.SURF的特征检测方法,但是正常下载安装OpenCV并不包含附加库,因为附加库 ...
- 该怎样提高ZBrush的创作效率
ZBrush是一款数字雕刻和绘画软件,以强大的功能和直观的工作流程改变了整个三维行业,相信使用ZBrush的人都希望加快雕刻速度提高ZBrush技能,很多雕刻专家也都试图证明加快雕刻速度是否真的能提 ...
- 边工作边刷题:70天一遍leetcode: day 84-1
Verify preorder sequence of Binary Search Tree 要点:simulating preorder sequence: 根据preorder的特点,首先会一直向 ...
- CSU 1081 集训队分组
题意:有n个学生,比了一场比赛,但是榜单看不到了.现在告诉你m段信息,每段信息的内容是(a,b),表示a的排名比b的高.问你能不能根据这些信息得出这场比赛的前k名. 思路:用拓扑排序找出一组符合k个人 ...
- VA助手(Visual Assist X) 笔记
官方网站:http://www.wholetomato.com/ 一. 快捷键 Shift+Alt+F Find References 查找引用 Shift+Alt+S FindSynbolDialo ...