- 博客(10)
- 收藏
- 关注
原创 scala隐式转换--2.隐式值和隐式参数
/** * 定义一个类,有一个隐式参数的方法 */class Boys { def getAge(name: String)(implicit map: HashMap[String, Int]) = { val age = map.getOrElse(name, null) println(name + "的年龄是:" + age) }}调用objec...
2018-12-22 21:44:58
134
原创 scala隐式转换--1.隐式函数和隐式类
定义隐式函数和类object ImplicitContext { //隐式转换函数(String->Int) implicit def string2Int(s:String)=s.toInt //隐式转换类,将 int类型转为Buffer 并将int值添加到buffer中 implicit class Int2String(i:Int){ def int2Bu...
2018-12-22 21:37:40
158
原创 scala设计模式--装饰模式
装饰模式/** * 水果特质,定义价格和名称两个抽象方法 */trait Fruits { def price(): Double def name(): String}/** * 定义三个特质作为水果子类 */trait Apple extends Fruits { abstract override def price() = super.price...
2018-12-22 01:17:41
477
原创 scala中self type 与 依赖注入
准备1.self type/** * * self => * self 并非关键字,你可以用任意其他字符替代 * self只是给this 关键字起了一个别名 * @author Yyyyy * @version 1.0 * **//** * 在内部类中的应用 */object World extends App { //当前对象 thi...
2018-12-22 01:14:18
309
原创 scala设计模式--观察者模式
观察者模式/** * 封装消息 * * @param oder * @param num * @param weapon */case class Message(oder: String, num: Int, weapon: String)/** * 被观察者(上级) 拥有注册,移除,通知的方法 */trait Leader { def regist...
2018-12-20 19:51:59
234
原创 scala模式设计--策略模式
策略模式/** * 策略模式: * 1.scala 实现策略模式很简单 用函数定义策略 * 2.定义一个类接收函数 * * @author Yyyyy * @version 1.0 * **//** * 操作数据的环境类(收银员) * * @param discount * @param originalPrice */class Cas...
2018-12-20 17:15:10
582
原创 scala设计模式--适配器模式
适配器模式/** * * 隐式转换实现适配器模式 * 适配器模式可以通过隐士转换,将不符合预期的现有类转换为目标接口 * 该模式对集成已经存在的各个组件很有用。 * * @author Yyyyy * @version 1.0 * **//** * 现有的标准电源,输出220电压 */class PowerSource { val outPu...
2018-12-20 14:08:32
249
原创 Scala设计模式--工厂模式
@Scala-设计模式工厂模式简单工厂模式/** * 汽车的共同特质 */trait Car{ def run()}/** * 创建一个宝马类 */class BaoMa extends Car { override def run(): Unit = println("宝马车开走了")}/** * 创建一个奔驰类 */class Be
2018-12-19 22:37:37
1007
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人