Scala语言编程初入门
来源:互联网 发布:运动软件电脑版 编辑:程序博客网 时间:2024/04/29 22:45
最近为了准备实习一直在自学Scala语言。由于时间和精力有限(一边准备期末考试,一边准备各种汇报大作业的日子终于要器去不复返啦,hiahiahia),只是学习了Scala语言的基础语法部分,为了方便日后继续深入,现在先记录下来。如果有错误,欢迎大家批评指正,我一定及时改正。
(ps. 参考资料 菜鸟教程,http://www.runoob.com/)
在一个类中利用函数来封装各个模块
import scala.util.control._;object mycode { def main(args: Array[String]) { //在 Scala 中,使用关键词 "var" 声明变量,使用关键词 "val" 声明常量。 //变量声明格式 var VariableName : DataType [= Initial Value] var myvar:String="world"; //变量 val myval:String="hello";//常量 //关于字符串的方法 var len = myvar.length();//使用 length() 方法来获取字符串长度 println("before change:content of myvar:`"+myvar+"length of it :"+len); // 输出w orld len = myvar.length();//使用 length() 方法来获取字符串长度 myvar=myvar.concat(myval);//链接字符串 len=myvar.length(); println("After change:content of myvar:"+myvar+"length of it "+len); // 输出world hello //元组 var two_e=(20,"hello"); println(two_e); //字符串遍历赋值 var X:Array[Int]=new Array[Int](5); var i:Int=1; for (i<-0 to 4) X(i)=i; printint(X(1),X(2),X(3)); //输出 //判断if.....else...... var x=20; judgement(x); //while循环 loop(x); //multiplier匿名函数 println("multiplier:"+multiplier(1)); //about arrey 关于数组的函数 about_arrey(); }def loop(x:Int){ var y:Int=x; var loop=new Breaks;//使用break来结束循环 loop.breakable { while(y<30) { if (y==25) { println(x); loop.break; } else y=y+1; } }}def judgement(x:Int)//判断x是否等于20{ if (x<20) println("x<20"); else if (x==20) println("x==20"); else println("x>20");} def printint(x:Int*)//可变参数函数1{ var i:Int=0; var X:Int=1; for (X<-x) { println("No."+i+":"+X); i=i+1; } } var free:Int=3;val multiplier= (i:Int) =>i*3;def about_arrey()//数组赋值与遍历{ var mylist=new Array[Float](5); var i:Int=0; for (i<-0 to 4) mylist(i)=66.66; for (i<- 0 to 4) println("%f",mylist(i)); }}
利用类来封装
import java.io._import scala.util.matching.Regeximport java.io.FileReaderimport java.io.FileNotFoundExceptionimport java.io.IOExceptionimport scala.io.Sourceclass point(x:Int,y:Int)//父类{ var px:Int=x; var py:Int=y; def move(dx:Int,dy:Int) { px=px+dx; py=py+dy; println("now position:"+px+" "+py); }}class location (var x:Int,var y:Int,z:Int) extends point(x,y)//子类{ var pz:Int=z; def move(dx:Int,dy:Int,dz:Int)//方法重载 { px=px+dx; py=py+dy; pz=pz+dz; println("now position:"+px+" "+py+" "+pz); } override def move (dx:Int,dy:Int)//重写非抽象方法时必须加override { println("want to move:"+dx,dy); }}class about_match(var input:Any) //模式匹配{ var x:Any=input; def match_test() : Any =this.x match { case 1 => "math 1"; case "math 1" => 1; }}trait Equal//接口{ def isEqual(x:Int):Boolean; def isNotEqual(x:Int):Boolean;}class equal_or_not(val x:Int) extends Equal//接口的实现{ val pc:Int=x; def isEqual(x:Int) = this.pc == x; def isNotEqual(x:Int) = this.pc != x;}class about_file//文件读写(){ def readfile(filename:String)//读文件并输出 { Source.fromFile(filename ).foreach { print } } def writefile(filename:String) { print("please input data: " ); val line = readLine();//读取输入数据 val writer = new PrintWriter(new File(filename ));//创建写文件类 writer.write(line); writer.close(); } }class about_ex//关于异常 try....catch....finally{ def error(file:String) { try { val f = new FileReader("file") } catch { case ex: FileNotFoundException => { println("Missing file exception") } case ex: IOException => { println("IO Exception") } } finally { println("Exiting finally...") } }}class about_tiqu//提取{ def tiqu( ) { println ("Apply 方法 : " + apply("Zara", "gmail.com")); println ("Unapply 方法 : " + unapply("Zara@gmail.com")); println ("Unapply 方法 : " + unapply("Zara Ali")); } // 注入方法 (可选) def apply(user: String, domain: String) = { user +"@"+ domain } // 提取方法(必选) def unapply(str: String): Option[(String, String)] = { val parts = str split "@" if (parts.length == 2){ Some(parts(0), parts(1)) }else{ None } }}class about_class private(val color:String)//私有构造方法{ println("hello,world\n"); }//伴生对象,与类共享名字,可以访问类的私有属性和方法object about_class{ def main(args:Array[String]) { val pt = new point(20,10);//创建对象 pt.move(10,10); val pr =new location(10,10,20);//创建子类对象 pr.move(20,20,20); pr.move(20,30); val pl=new about_class("red"); var equal=new equal_or_not(20);//演示接口的使用 println(equal.isNotEqual(30)); var ma=new about_match(1);//演示模式匹配 println(ma.match_test()); var pattern=new Regex("(S|s)cala");//正则表达式 val str = "scala is cool,Scala is big cool"; println((pattern findAllIn str).mkString(",")); println(pattern replaceFirstIn(str,"Java")); var test=new about_ex();//演示异常 test.error("test.txt"); var test_tiqu=new about_tiqu();//演示提取 test_tiqu.tiqu(); var file=new about_file();//演示文件读写 file.writefile("input.txt"); file.readfile("input.txt"); }}
阅读全文
1 0
- Scala语言编程初入门
- Scala语言初入门
- 炼数成金Scala语言入门
- Scala语言开发入门
- scala语言入门
- 炼数成金Scala语言入门
- Scala编程语言
- Scala 编程语言
- 多面编程语言Scala
- 多面编程语言Scala
- Scala 开启Scala编程语言之旅
- Scala编程语言视频教程|Scala视频
- scala语言基础入门(1)
- Scala入门之函数编程
- Scala入门之函数编程
- scala 函数式编程入门
- scala编程入门(理解)
- Scala编程语言中的下划线
- tensorflow中下载mnist数据集
- Android Studio地图开发(百度地图)
- 【Linux】中的生产者与消费者的关系
- SpringFramework之@AliasFor
- this和super
- Scala语言编程初入门
- Android EditView自定义正则表达式
- 使用zbar库对预览界面进行裁剪解码时遇到的坑
- HDU5248(二分+模拟)
- Java加载机制详细说明
- OkHttp 3.7源码分析(三)——任务队列
- imgproc模块--直方图
- 单例模式思考
- 如何解决failed to push some refs to git