1. def main(args : Array[String]): Unit =
  2. {
  3. def add(x:Int,y:Int):Int =
  4. {
  5. return x+y;
  6. }
  7.  
  8. def subtract:(Int,Int)=>Int = (x,y)=>x-y;
  9.  
  10. val multiply = (x:Int,y:Int)=>x*y;
  11.  
  12. val division :(Int,Int)=>Float = (x,y)=>x/y;
  13.  
  14. def sum:Int=>Int = i => i match
  15. {
  16. case 1 => 1;
  17. case n => n + sum(i-1);
  18. }
  19.  
  20. def summ(i:Int):Int= i match
  21. {
  22. case 1 => 1;
  23. case n => n + summ(n-1);
  24. }
  25.  
  26. lazy val fact:Int=>Int =
  27. {
  28. case 1 => 1;
  29. case n => n*fact(n-1);
  30. }
  31.  
  32. lazy val summary:Int=>Int = (_:Int) match
  33. {
  34. case 1 => 1;
  35. case n => n + summary(n-1);
  36. }
  37.  
  38. println(summ(100));
  39.  
  40. }

  

  1. object Main{
  2.  
  3. def main(args:Array[String]):Unit=
  4. {
  5.  
  6. object W
  7. {
  8. type S = String;
  9. type D = Tuple2[S,S];
  10. def say(x:D):S= s"say ${x _1},${x _2}";
  11. }
  12.  
  13. implicit def MyStr(s:String)=new
  14. {
  15. def ++:(x:Char) = x + s;
  16. }
  17.  
  18. val ss = 'c' ++: "hello"; // 相当于 "hello".++:('c')
  19. //但是如果写成 "hello" ++: 'c' 则提升 Char 没有 ++: 方法
  20.  
  21. Predef println ss
  22.  
  23. }
  24. }

  

  1. implicit def XString(s:String)= new
  2. {
  3. def unary_- = s.reverse;
  4. def ? = s.length;
  5. }
  6.  
  7. println(-"hello");//olleh
  8. println( "hello"? );//5

  

在基类中将基本的操作(开\关\异常处理)都处理好,在子类中只定义功能操作.

  1. package exp;
  2.  
  3. import java.io.PrintWriter
  4.  
  5. object Main
  6. {
  7. def main(args : Array[String]): Unit =
  8. {
  9. def fileOps = new MyFileOps;
  10. fileOps.opPw;
  11. }
  12.  
  13. }
  14.  
  15. class MyFileOps extends FileOps("/home/wengmj/1.txt")
  16. {
  17. override def pwOp(writer:PrintWriter):Unit=writer.println("hello world");
  18. }
  19.  
  20. abstract class FileOps(path:String)
  21. {
  22. def pwOp(writer:PrintWriter):Unit;
  23.  
  24. def opPw():Unit=
  25. {
  26. val writer = new PrintWriter(path);
  27. try
  28. {
  29. this.pwOp(writer);
  30. }
  31. catch
  32. {
  33. case _:Throwable => println("unknow exception");
  34. }
  35. finally
  36. {
  37. writer.close
  38. }
  39. }
  40. }

编写新的控制结构:借贷模式,柯里化

  1. package exp
  2. import java.sql.SQLException
  3.  
  4. object Main {
  5. def main(args: Array[String]): Unit = {
  6.  
  7. val x = withDB(new DB()) {//这里本应该用小括号的用大括号是函数只有有个参数时用大括号更优美
  8. db =>
  9. {//这个大括号是定义函数必须的 
  10. db.prepareStatement("update products set product_name='aaa' where product_id=123");
  11. db.update
  12. }
  13. }
  14. }
  15.  
  16. def withDB(db: DB)(op: DB => Any): Any =
  17. {
  18. var r: Any = null;
  19. try {
  20. db.open
  21. r = op(db);
  22. db.close
  23. } catch {
  24. case e: Exception => println(e.getMessage);
  25. } finally {
  26. db.close
  27. }
  28. r;
  29. }
  30. }
  31. class DB {
  32. @throws(classOf[SQLException])
  33. def prepareStatement(sql: String) = Unit;
  34.  
  35. @throws(classOf[SQLException])
  36. def open() = Unit;
  37.  
  38. def close(): Unit = Unit;
  39.  
  40. @throws(classOf[SQLException])
  41. def update(): Boolean = false;
  42. }

  

Scala 学习笔记(五)的更多相关文章

  1. [Scala]Scala学习笔记五 Object

    1. 单例对象 Scala没有静态方法或静态字段,可以使用object来达到这个目的,对象定义了某个类的单个实例: object Account{ private var lastNumber = 0 ...

  2. scala 学习笔记五 foreach, map, reduce

    例子 val v = Vector(,,,) ) println(s) //输出:Vector(2, 4, 6, 8) val v2 = Vector(,,,) var v3 = v2.reduce( ...

  3. C#可扩展编程之MEF学习笔记(五):MEF高级进阶

    好久没有写博客了,今天抽空继续写MEF系列的文章.有园友提出这种系列的文章要做个目录,看起来方便,所以就抽空做了一个,放到每篇文章的最后. 前面四篇讲了MEF的基础知识,学完了前四篇,MEF中比较常用 ...

  4. (转)Qt Model/View 学习笔记 (五)——View 类

    Qt Model/View 学习笔记 (五) View 类 概念 在model/view架构中,view从model中获得数据项然后显示给用户.数据显示的方式不必与model提供的表示方式相同,可以与 ...

  5. java之jvm学习笔记五(实践写自己的类装载器)

    java之jvm学习笔记五(实践写自己的类装载器) 课程源码:http://download.csdn.net/detail/yfqnihao/4866501 前面第三和第四节我们一直在强调一句话,类 ...

  6. Learning ROS for Robotics Programming Second Edition学习笔记(五) indigo computer vision

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...

  7. Typescript 学习笔记五:类

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  8. ES6学习笔记<五> Module的操作——import、export、as

    import export 这两个家伙对应的就是es6自己的 module功能. 我们之前写的Javascript一直都没有模块化的体系,无法将一个庞大的js工程拆分成一个个功能相对独立但相互依赖的小 ...

  9. muduo网络库学习笔记(五) 链接器Connector与监听器Acceptor

    目录 muduo网络库学习笔记(五) 链接器Connector与监听器Acceptor Connector 系统函数connect 处理非阻塞connect的步骤: Connetor时序图 Accep ...

  10. python3.4学习笔记(五) IDLE显示行号问题,插件安装和其他开发工具介绍

    python3.4学习笔记(五) IDLE显示行号问题,插件安装和其他开发工具介绍 IDLE默认不能显示行号,使用ALT+G 跳到对应行号,在右下角有显示光标所在行.列.pycharm免费社区版.Su ...

随机推荐

  1. Uva 10891 经典博弈区间DP

    经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...

  2. Linux and the Device Tree

    来之\kernel\Documentation\devicetree\usage-model.txt Linux and the Device Tree ----------------------- ...

  3. 第一、初识C语言

    1·C语言强大而灵活,如python,LISP,FORTRAN,Perl,Logo,BASIC,PASACAL的编译器和解释器都是C语言编写的. 2·C语言的指针错误往往难以察觉,但这恰好告诉我们,一 ...

  4. Ubuntu下的Notepad++:Notepadqq

    http://www.linuxidc.com/Linux/2015-07/120678.htm 适合从Win平台转移到Linux平台的用户,如果你之前一直再Win下使用nodepad++, 推荐你再 ...

  5. django queryset values&values_list

    values返回是字典列表; values_list返回的是元组列表, values_list加上 flat=True 1 1 之后返回值列表

  6. 剑指offer七:两个链表的第一个公共结点

    输入两个链表,找出它们的第一个公共结点. import java.util.*; public class Solution { public ListNode FindFirstCommonNode ...

  7. 关于win10连接不上ftp的解决方案

    win10系统连接ftp服务器的时候,会先出现假死,比如: 然后 就会报错: 面对这些问题:我们不需要关闭放火请,卸载杀毒软件等等无用的操作,只需要一步就能搞定: 把ftp:// 换成 file:\\ ...

  8. 第三方的图片加载( Android-Universal-Image-Loader)

    Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示. (1).使用多线程加载图片(2).灵活配置Im ...

  9. Strus2学习:基础(一)

    Strus2基础: Sturs2起源以及背景: 在起源很早(2002年左右)的 strus1 和 webWork 基础上进行扩展,并且兼容这两大框架!总之很好用啦,随着学习的深入,应该会有更好的诠释的 ...

  10. linux 记录用户操作记录日志

    ####################################################################################historyUSER_IP=$ ...