def main(args : Array[String]): Unit =
{
def add(x:Int,y:Int):Int =
{
return x+y;
} def subtract:(Int,Int)=>Int = (x,y)=>x-y; val multiply = (x:Int,y:Int)=>x*y; val division :(Int,Int)=>Float = (x,y)=>x/y; def sum:Int=>Int = i => i match
{
case 1 => 1;
case n => n + sum(i-1);
} def summ(i:Int):Int= i match
{
case 1 => 1;
case n => n + summ(n-1);
} lazy val fact:Int=>Int =
{
case 1 => 1;
case n => n*fact(n-1);
} lazy val summary:Int=>Int = (_:Int) match
{
case 1 => 1;
case n => n + summary(n-1);
} println(summ(100)); }

  

object Main{

  def main(args:Array[String]):Unit=
{ object W
{
type S = String;
type D = Tuple2[S,S];
def say(x:D):S= s"say ${x _1},${x _2}";
} implicit def MyStr(s:String)=new
{
def ++:(x:Char) = x + s;
} val ss = 'c' ++: "hello"; // 相当于 "hello".++:('c')
//但是如果写成 "hello" ++: 'c' 则提升 Char 没有 ++: 方法 Predef println ss }
}

  

 implicit def XString(s:String)= new
{
def unary_- = s.reverse;
def ? = s.length;
} println(-"hello");//olleh
println( "hello"? );//5

  

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

package exp;

import java.io.PrintWriter

object Main
{
def main(args : Array[String]): Unit =
{
def fileOps = new MyFileOps;
fileOps.opPw;
} } class MyFileOps extends FileOps("/home/wengmj/1.txt")
{
override def pwOp(writer:PrintWriter):Unit=writer.println("hello world");
} abstract class FileOps(path:String)
{
def pwOp(writer:PrintWriter):Unit; def opPw():Unit=
{
val writer = new PrintWriter(path);
try
{
this.pwOp(writer);
}
catch
{
case _:Throwable => println("unknow exception");
}
finally
{
writer.close
}
}
}

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

package exp
import java.sql.SQLException object Main {
def main(args: Array[String]): Unit = { val x = withDB(new DB()) {//这里本应该用小括号的用大括号是函数只有有个参数时用大括号更优美
db =>
{//这个大括号是定义函数必须的 
db.prepareStatement("update products set product_name='aaa' where product_id=123");
db.update
}
}
} def withDB(db: DB)(op: DB => Any): Any =
{
var r: Any = null;
try {
db.open
r = op(db);
db.close
} catch {
case e: Exception => println(e.getMessage);
} finally {
db.close
}
r;
}
}
class DB {
@throws(classOf[SQLException])
def prepareStatement(sql: String) = Unit; @throws(classOf[SQLException])
def open() = Unit; def close(): Unit = Unit; @throws(classOf[SQLException])
def update(): Boolean = false;
}

  

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. Android动态方式破解apk前奏篇(Eclipse动态调试smail源码)

    一.前言 今天我们开始apk破解的另外一种方式:动态代码调试破解,之前其实已经在一篇文章中说到如何破解apk了: Android中使用静态方式破解Apk  主要采用的是静态方式,步骤也很简单,首先使用 ...

  2. What technical details should a programmer of a web application consider before making the site public?

    What things should a programmer implementing the technical details of a web application consider bef ...

  3. Keras

    sudo pip install keras --安装 新建一个文件,里面存储的数据:第一列是属性,第二列是类别 11220044 011220044 011220044 011220033 1112 ...

  4. 介绍一个可以将Expression表达式树解析成Transact-SQL的项目Expression2Sql

    一.Expression2Sql介绍 Expression2Sql是一个可以将Expression表达式树解析成Transact-SQL的项目.简单易用,几分钟即可上手使用,因为博主在设计Expres ...

  5. dubbo配置文件报错解决方案

    下载dubbo.xsd 文件 在eclipse->window->perferences->XML Catalog->Add ->File system->选择刚才 ...

  6. Android 基础概念了解

    Android 的前世今生Android 系统框架Android 主要组成 部分Android 常用的操作 Android 的前世今生 Android 的诞生 2003年10月,有"Andr ...

  7. MySQL5.0+提示字段没有默认值(doesn’t have a default value)的解决方法

    方法一: 打开my.ini,查找  sql-mode=”STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION” 修改为  sql ...

  8. Mac下遇到 'reading initial communication packet’ 问题

    今天在开发过程中,一个单位跑的好好的项目,在家中的Mac下运行时,遇到了下面这个错误:   "Lost connection to MySQL server at 'reading init ...

  9. 使用注解方式生成Hibernate映射文件

    @Entity:表示是一个hibernate的实体类 @Table:表示实体类和表的对应关系 @Id:表示是数据库中的主键 @Column:在数据表中描述的对应的列的信息 属性名是根据get方法,数据 ...

  10. 下载安装JDK,配置环境变量

    Hello,JDK; 在开始学习JAVA之前,第一件事情肯定是被告知:先下载JDK.就像我的一个朋友问我的一样"JDK是个什么鬼?我学的不是JAVA么,为什么要下载JDK?". J ...