scala学习笔记(高级特性1)
2016-02-01 10:45
495 查看
一、一切皆为对象
对于基础篇中写道的在scala中一切皆为对象或许大家理解还是不很清楚,举个例子:
在scala中没有运算符的概念,相应的是方法的概念。
被看成是调用1的带有2变量的+方法,即1.+(2),
二、伴生对象
在scala中没有static变量,而为了弥补这一空缺,scala存在着伴生对象的概念,即在伴生对象中定义的方法可以在类中直接调用。
解析:apply是Student类的伴生对象中的方法,在调用Student类初始化时,会调用派生对象中的apply方法,给Student类赋值,避免使用new对象了。
对于apply方法:生成新对象时自动掉用。
三、高级特性
1.Array
赋值:
2.list
list操作方法
3.tuples
4.sets和maps
set包含:
scala.collection.mutable.Set;
scala.collection.immutable.HashSet;
maps包含:
scala.collection.mutable.Map
5.File读取
对于基础篇中写道的在scala中一切皆为对象或许大家理解还是不很清楚,举个例子:
在scala中没有运算符的概念,相应的是方法的概念。
1+2
被看成是调用1的带有2变量的+方法,即1.+(2),
for(i<- 1 to 3) print(i) 等价于 for(i<- 1.to(3)) print(i)
二、伴生对象
在scala中没有static变量,而为了弥补这一空缺,scala存在着伴生对象的概念,即在伴生对象中定义的方法可以在类中直接调用。
class Student(var name:String,var age:Int){ } object Student{ def apply( pname:String, page:Int)=new Student(pname,page) } var s = Student("zhangsan",15) print(s.name)
解析:apply是Student类的伴生对象中的方法,在调用Student类初始化时,会调用派生对象中的apply方法,给Student类赋值,避免使用new对象了。
对于apply方法:生成新对象时自动掉用。
三、高级特性
1.Array
赋值:
var arrays:Array[String] = new Array[String](2) arrays(0)="abc" 等价于 arrays(0).update("abc") var arr = Array("a","b","c") 等价于 var arr = Array.apply("a","b","c")
2.list
val oneTwo = List(1, 2) val threeFour = List(3, 4) val oneTwoThreeFour = oneTwo ::: threeFour print(oneTwoThreeFour)
val oneTwoThree = 1 :: 2 :: 3 :: Nil println(oneTwoThree)
list操作方法
名称 | 作用 |
---|---|
List() or Nil | The empty List |
List(“Cool”, “tools”, “rule”) | Creates a new List[String] with the three values “Cool”, “tools”, and “rule” |
val thrill = “Will” :: “fill” ::”until” :: Nil | Creates a new List[String] with the three values “Will”, “fill”, and”until” |
List(“a”, “b”) ::: List(“c”, “d”) | Concatenates two lists (returns a new List[String] with values “a”, “b”,”c”, and “d”) |
thrill(2) | Returns the element at index 2 (zero based) of the thrill list (returns “until”) |
thrill.count(s => s.length == 4) | Counts the number of string elements in thrill that have length 4 (returns 2) |
thrill.drop(2) | Returns the thrill list without its first 2 elements (returns List(“until”)) |
thrill.dropRight(2) | Returns the thrill list without its rightmost 2 elements (returns List(“Will”)) |
thrill.exists(s => s == “until”) | Determines whether a string element exists in thrill that has the value “until” (returns true) |
thrill.filter(s => s.length == 4) | Returns a list of all elements, in order, of the thrill list that have length 4 (returns List(“Will”, “fill”)) |
thrill.forall(s =>s.endsWith(“l”)) | Indicates whether all elements in the thrill list end with the letter “l” (returns true) |
thrill.foreach(s => print(s)) | Executes the print statement on each of the strings in the thrill list (prints “Willfilluntil”) |
val pair = (99, "Luftballons") println(pair._1) println(pair._2)
4.sets和maps
set包含:
scala.collection.mutable.Set;
scala.collection.immutable.HashSet;
import scala.collection.mutable.Set val movieSet = Set("Hitch", "Poltergeist") movieSet += "Shrek" println(movieSet)
maps包含:
scala.collection.mutable.Map
import scala.collection.mutable.Map val treasureMap = Map[Int, String]() treasureMap += (1 -> "Go to island.") treasureMap += (2 -> "Find big X on ground.") treasureMap += (3 -> "Dig.") println(treasureMap(2)) 或 val romanNumeral = Map( 1 -> "I", 2 -> "II", 3 -> "III", 4 -> "IV", 5 -> "V" ) println(romanNumeral(4))
5.File读取
var longestLine = lines.reduceLeft((a,b) => if(a.length >b.length) a else b) def maxWidth = widthOfLength(longestLine) def widthOfLength(s:String)=s.length.toString.length for (line <- lines) { val numSpaces = maxWidth - widthOfLength(line) val padding = " " * numSpaces println(padding + line.length +" | "+ line) }
相关文章推荐
- ResourceManager相关配置参数
- jquery 日期控件datepicker属性详细解析
- JAVA解析JSON数据
- iOS常用快捷键总结
- android的Log日志打印管理工具类(一)
- jsp+oracle 排序分页+Pageutil类
- 单例正确写法
- android shape的使用
- 创建XmlPullParser两种方式
- 设置ip地址,vm设置mac与ip
- SimpleDraweeView 控件清空缓存
- 实现垂直居中的一种好用的方法
- 解决easyui-tab添加tab滚动条问题
- 自动更新
- PHP中替换换行符方法总结
- CardView 的自定义属性
- Android APP 退出时 “你敢再按一次,我就退出了!” 功能实现
- jGgrid设置垂直滚动条以及显示高度
- 2月风怒(3)
- Android从相册选取图片并裁剪