Scala 学习笔记(五)
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 学习笔记(五)的更多相关文章
- [Scala]Scala学习笔记五 Object
1. 单例对象 Scala没有静态方法或静态字段,可以使用object来达到这个目的,对象定义了某个类的单个实例: object Account{ private var lastNumber = 0 ...
- scala 学习笔记五 foreach, map, reduce
例子 val v = Vector(,,,) ) println(s) //输出:Vector(2, 4, 6, 8) val v2 = Vector(,,,) var v3 = v2.reduce( ...
- C#可扩展编程之MEF学习笔记(五):MEF高级进阶
好久没有写博客了,今天抽空继续写MEF系列的文章.有园友提出这种系列的文章要做个目录,看起来方便,所以就抽空做了一个,放到每篇文章的最后. 前面四篇讲了MEF的基础知识,学完了前四篇,MEF中比较常用 ...
- (转)Qt Model/View 学习笔记 (五)——View 类
Qt Model/View 学习笔记 (五) View 类 概念 在model/view架构中,view从model中获得数据项然后显示给用户.数据显示的方式不必与model提供的表示方式相同,可以与 ...
- java之jvm学习笔记五(实践写自己的类装载器)
java之jvm学习笔记五(实践写自己的类装载器) 课程源码:http://download.csdn.net/detail/yfqnihao/4866501 前面第三和第四节我们一直在强调一句话,类 ...
- Learning ROS for Robotics Programming Second Edition学习笔记(五) indigo computer vision
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...
- Typescript 学习笔记五:类
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- ES6学习笔记<五> Module的操作——import、export、as
import export 这两个家伙对应的就是es6自己的 module功能. 我们之前写的Javascript一直都没有模块化的体系,无法将一个庞大的js工程拆分成一个个功能相对独立但相互依赖的小 ...
- muduo网络库学习笔记(五) 链接器Connector与监听器Acceptor
目录 muduo网络库学习笔记(五) 链接器Connector与监听器Acceptor Connector 系统函数connect 处理非阻塞connect的步骤: Connetor时序图 Accep ...
- python3.4学习笔记(五) IDLE显示行号问题,插件安装和其他开发工具介绍
python3.4学习笔记(五) IDLE显示行号问题,插件安装和其他开发工具介绍 IDLE默认不能显示行号,使用ALT+G 跳到对应行号,在右下角有显示光标所在行.列.pycharm免费社区版.Su ...
随机推荐
- AngularJS使用指南
ng-app 定义一个AngularJS应用程序 ng-model 把元素值绑定到AngularJS应用程序 ng-blind 把AngularJS应用程序数据绑定到HTML视图上 ng-init 初 ...
- 更新maven库
删除所在本地仓库的文件夹,例如: 然后,重新下载一个jar包,放到工程的文件夹,注意名字要不同,否则maven库不予更新,替换完成后,点击工程,maven,update project即可,再次查看M ...
- servlet上传下载(任何格式的都可以)
jar不能低于此版本,JDK1.6以上,否则户报错 <dependency> <groupId>commons-fileupload</groupId> <a ...
- java(2)之前往对象村
这次说一说面向对象与面向过程的区别以及面向对象的优点.
- 解迷宫的C++的未完善编程代码........请大神们帮忙改善下.........
这...................................................................... 我也是醉了 看不太懂,大神们求解............ ...
- 不定长链表队列C语言实现
#ifndef _CONST_H_#define _CONST_H_ #include <stdio.h>#include <stdlib.h> typedef enum { ...
- MyEclipse中的SVN操作手册
原文出处:http://blog.sina.com.cn/s/blog_8a3d83320100zhmp.html 1.导入项目 点击工具栏上的[File-Import],进入下图 (如果你的对话框中 ...
- matlab basic operation command
Matlab basic operation: >> 5+6 ans = 11 >> 3*4 ans = 12 >> 2^6 ans = 64 >> 1 ...
- caffe + ubuntu16.04 (version without GPU)
This Guide is based on caffe github wiki guide (https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-or-1 ...
- 1006. Sign In and Sign Out (25)
At the beginning of every day, the first person who signs in the computer room will unlock the door, ...