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 ...
随机推荐
- win10搭建代理服务器实现绕过校园网的共享限制--从入门到放弃
博主所在学校特别坑爹,校园网被电信一家垄断了,而且最恶心的还是电信要求一条网线只能供一台电脑上网,不许接路由器共享网络= =- (还有电信2M价格是380+每年,20m是500每年,而且网速都很慢= ...
- zju 1937 初涉——深度优先搜索
#include "stdio.h" int a[11],b[11]; int k,flag,n,s; void DFS(); int main() { int i; while( ...
- sql 查询基本语法
1.计算列 select * from emp --*表示所有的 --from emp 表示从emp表查询 select empno,enam ...
- Storm wordcount Read from file
source code: package stormdemo; import java.io.BufferedReader; import java.io.BufferedWriter; import ...
- django中的站点管理
所谓网页开发是有趣的,管理界面是千篇一律的.所以就有了django自动管理界面来减少重复劳动. 一.激活管理界面 1.django.contrib包 django自带了很多优秀的附加组件,它们都存在于 ...
- Visualize real-time data streams with Gnuplot
源文地址 (September 2008) For the last couple of years, I've been working on European Space Agency (ESA) ...
- CEPH块设备创建及快照
1.创建image rbd create foo --size 1024 {--image-format 2}//创建一个名为foo的image,大小为1024M,当需要克隆快照时,需要添加大括号中的 ...
- IOS Orientation, 想怎么转就怎么转~~~
此博文主要针对IOS应用, 是屏幕旋转相关问题的一个总结. 主要内容有: IOS5,6,7不同版的适配. 强制旋转和自动旋转. 博客: http://www.cnblogs.com/jhzhu 邮箱: ...
- is A和has A的区别
is A has A Red is A Color: class Red extends Color{} class Blue extends Color{} class Yellow exrends ...
- java 16 -4 LinkedList的特有功能
了解 LinkedList的特有功能: A:添加功能 public void addFirst(Object e) public void addLast(Object e) B:获取功能 publi ...